<?php

/*************************************************
 * TextDB 文本数据库
 *
 * 代码所有       PHPText.Net
 * 代码作者       黄创钦,tine2cx@163.com
 * 联系方式       QQ 233944397
 * 本代码开源使用，可以随意修改传播，但请保留本版权
 * 声明,谢谢合作！
 ************************************************/

/* 下列信息 请勿改动 */
/* 设置 《系统存储路径》 */
define("_TextDB_SQLPath_",             $_SERVER['DOCUMENT_ROOT']."/"._TextDB_SavePath_);

/* 设置 《系统稳定过滤内容》 */
define("_TextDB_SysFilter_DBN_",       "*,|,\\,:,\",<,>,?,/, ,.,#");   //数据库名称过滤
define("_TextDB_SysFilter_TBN_",       "*,|,\\,:,\",<,>,?,/, ,.,#");   //数据表名称过滤
define("_TextDB_SysFilter_TBC_",       "\n,\r,\t,"._TextDB_EXStr_);   //数据表内容过滤
define("_TextDB_SysFilter_TBCR_",      "\n/,\r/,\t/,"._TextDB_REXStr_);   //数据表内容过滤

/* 设置 《系统基本信息扩展》 */
define("_TextDB_ResourceLogo_Size_",   strlen(_TextDB_ResourceLogo_));

/* 设置 《系统表初始信息》 */
define("_TextDB_SysTBLF_UserInfo_",       "用户名,密码,邮件,权限,状态,开通日期,截止日期,上次登陆IP,上次登陆时间,拥有库"); //设置 用户信息表初始字段[全]。（默认："编号,用户名,密码,邮件,权限,状态,开通日期,截止日期,上次登陆IP,上次登陆时间,拥有库"）
define("_TextDB_SysTBLF_DataBaseList_",   "库名,建立者,拥有者,开放,表数,行数,建立日期,回收日期,还原日期"); //设置 数据库清单初始字段[全]。（默认："编号,库名,拥有者,开放,计数,行数,建立日期"）
define("_TextDB_SysTBLF_BakDataBaseList_","库名,建立者,拥有者,开放,表数,行数,建立日期,回收日期,还原日期"); //设置 数据库清单初始字段[全]。（默认："编号,库名,拥有者,开放,计数,行数,建立日期"）
define("_TextDB_SysTBLF_AllTableList_",   "表名,建立者,所属库,开放,计数,行数,建立日期,回收日期,还原日期,当前状态"); //设置 数据表清单初始字段[全]。（默认："编号,表名,所属库,开放,计数,行数,建立日期"）
define("_TextDB_SysTBLF_DBTableList_",    "表名,建立者,所属库,开放,计数,行数,建立日期,回收日期,还原日期,当前状态"); //设置 数据表清单初始字段[全]。（默认："编号,表名,所属库,开放,计数,行数,建立日期"）
define("_TextDB_SysTBLF_TableList_",      "表名,建立者,所属库,开放,计数,行数,建立日期,回收日期,还原日期"); //设置 数据表清单出事字段[库]。（默认："编号,表名,建立者,开放,计数,行数,建立日期"）
define("_TextDB_SysTBLF_BakTableList_",   "表名,建立者,所属库,开放,计数,行数,建立日期,回收日期,还原日期"); //设置 数据表清单出事字段[库]。（默认："编号,表名,建立者,开放,计数,行数,建立日期"）

/* 跟随 《系统库和表信息》 设置变动 */
/* 禁止信息设置 */
define("_TextDB_SysDBSN_",                _TextDB_SysDB_System_); //设置 禁止建立/删除/回收/修改 【数据库】 名称
define("_TextDB_SysTBSN_",                _TextDB_SysTBL_DataBaseList_.","._TextDB_SysTBL_BakDataBaseList_.","._TextDB_SysTBL_AllTableList_.","._TextDB_SysTBL_DBTableList_.","._TextDB_SysTBL_TableList_.","._TextDB_SysTBL_BakTableList_); //设置 禁止建立/删除/修改 【数据表】 名称
define("_TextDB_SysTBSN2_",               _TextDB_SysTBL_UserInfo_); //设置 用户  【数据表】 名称
define("_TextDB_SysTBLD_UserInfo_",       _TextDB_InitUserName_.",".md5(_TextDB_InitPassword_).","._TextDB_InitUserEmail_.",9,1,0000-00-00,0000-00-00,0.0.0.0,0000-00-00 00:00:00,*");

/* 写入数据 */
function TextDB_FileWrite($FileName,$WriteMode,$WirteContent)
{
	$FileHead=fopen($FileName,$WriteMode);
	flock($FileHead,LOCK_EX);
	fwrite($FileHead,$WirteContent);
	flock($FileHead,LOCK_UN);
	fclose($FileHead);
}

/**
 * 将文件转化成数组
 */
function TextDB_FileToArray($FileName)
{
	if(filesize($FileName)<_TextDB_EXPSize_)
	{
		$FileHead=fopen($FileName,"r");
		flock($FileHead,LOCK_EX);
		$FileContent=fread($FileHead,filesize($FileName)+1);
		flock($FileHead,LOCK_UN);
		fclose($FileHead);
		$FileContent = str_replace("\n","\n"._TextDB_EXStr_."TextDB"._TextDB_EXStr_,$FileContent);
		$FileArray   = explode(_TextDB_EXStr_."TextDB"._TextDB_EXStr_,$FileContent);
	}
	else
	{
		$FileArray   = file($FileName);
	}
	return $FileArray;
}

//获取文件中的第一行
//参数一 文件名
function TextDB_ReadOneLine($FileName)
{
	$FileHead = fopen($FileName,"r");
	flock($FileHead,LOCK_EX);
	$FileContent = '';
	rewind($FileHead);
	while(true)
	{
		$Str = fgetc($FileHead);
		if($Str!="\n" && !feof($FileHead))
		{
			$FileContent .= $Str;
		}
		else
		{
			break;
		}
	}

	flock($FileHead,LOCK_UN);
	fclose($FileHead);

	return $FileContent;
}

//输出目录文件
function TextDB_RMDIR($DIR)
{
	$mydirectory = opendir($DIR);
	while($entryname=readdir($mydirectory))
	{
		switch (true)
		{
			case ($entryname=="."):
				break;
			case ($entryname==".."):
				break;
			case (is_dir($DIR."/".$entryname)):
				TextDB_RMDIR($DIR."/".$entryname);
				break;
			default:
				unlink($DIR."/".$entryname);
				break;
		}
	}
	closedir($mydirectory);
}

//TEXTDB安全过滤内容
function TextDB_FilterStr($FilterStr,$Mode=1)
{
	$FilterMode1 = explode(",",_TextDB_SysFilter_TBC_);
	$FilterMode2 = explode(",",_TextDB_SysFilter_TBCR_);
	//判断模式
	if($Mode)  //过滤模式
	{
		$FilterStr = str_replace($FilterMode1,$FilterMode2,$FilterStr);
	}
	else  //还原模式
	{
		$FilterStr = str_replace($FilterMode2,$FilterMode1,$FilterStr);
	}
	return $FilterStr;
}

//检测是否存在
//参数一 检测字串
//参数二 被检测字串[数组，字串]
//参数三 是否忽略大小写[默认，否]
function TextDB_CheckStr($TextDB_CheckStr,$strs,$bs=0)
{
	//检测 被检测字串是否为数组
	if(!is_array($strs))
	{
		$strs = explode(",",$strs);
	}

	//检测 检测字串 是否 存在 被检测字串
	for($CheckNum=0;$CheckNum<sizeof($strs);$CheckNum++)
	{
		//判断 是否 忽略大小写
		if($bs)
		{
			//忽略大小写
			if(strtolower($TextDB_CheckStr)==strtolower($strs[$CheckNum]))
			{
				return true;
			}
		}
		else
		{
			//非忽略大小写
			if($TextDB_CheckStr==$strs[$CheckNum])
			{
				return true;
			}
		}
	}
	return false;
}

/**
 * 本函数用做检测数据查找，删除时值的条件
 * $Value 是 检测时所需的值
 */
function TextDB_CheckWhere($Value,$CMPValue)
{
	if(!empty($Value))
	{
		if(preg_match('/^\{V(.+)\}/', $Value, $Where))
		{
			if($Where['1']=='=')
			{
				$Value = str_replace($Where['0'],'',$Value);
				if($CMPValue==$Value)
				{
					return true;
				}
			}
			elseif($Where['1']=='!=')
			{
				$Value = str_replace($Where['0'],'',$Value);
				if($CMPValue!=$Value)
				{
					return true;
				}
			}
			elseif($Where['1']=='<')
			{
				$Value = str_replace($Where['0'],'',$Value);
				if($CMPValue<$Value)
				{
					return true;
				}
			}
			elseif($Where['1']=='<=')
			{
				$Value = str_replace($Where['0'],'',$Value);
				if($CMPValue<=$Value)
				{
					return true;
				}
			}
			elseif($Where['1']=='>')
			{
				$Value = str_replace($Where['0'],'',$Value);
				if($CMPValue>$Value)
				{
					return true;
				}
			}
			elseif($Where['1']=='>=')
			{
				$Value = str_replace($Where['0'],'',$Value);
				if($CMPValue>=$Value)
				{
					return true;
				}
			}
			elseif($Where['1']=='%' || $Where['1']=='%%')
			{
				$Value = str_replace($Where['0'],'',$Value);
				if(strstr($CMPValue,$Value))
				{
					return true;
				}
			}
			elseif($Where['1']=='%-')
			{
				$Value = str_replace($Where['0'],'',$Value);
				$VLen  = strlen($Value);
				if(substr($CMPValue,0,$VLen)==$Value)
				{
					return true;
				}
			}
			elseif($Where['1']=='-%')
			{
				$Value = str_replace($Where['0'],'',$Value);
				$VLen  = strlen($Value);
				if(substr($CMPValue,-$VLen)==$Value)
				{
					return true;
				}
			}
		}
		else
		{
			if($CMPValue==$Value)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
	else
	{
		return false;
	}
}

//按照字段排序
//参数一 排序数据内容(内容必须是TextDB查询数据格式)
//参数二 排序字段
function TextDB_OrderData($OrderData,$OrderFields)
{
	if($OrderData)
	{
		//数据大小
		$OrderDataSize = sizeof($OrderData);

		//获取字段数据进行排序
		foreach($OrderData as $key=>$Data)
		{
			$OrderList[$key] = $Data[$OrderFields];
		}

		asort($OrderList);

		//从新排序数据
		foreach($OrderList as $key=>$Data)
		{
			$TempOrderData[] = $OrderData[$key];
		}

		return $TempOrderData;
	}
}

/**
 * 本函数用于返回当前版本号
 */
function TextDB_Ver()
{
	return _TextDB_Ver_;
}

/**
 * 本函数用于识别错误代码类型
 */
function TextDB_GetErrorType($Code)
{
	$Code = strtoupper($Code);
	$Logo = substr($Code,0,1);
	if($Logo==='W')
	{
		$Type = 'Warning';
	}
	elseif($Logo==='E')
	{
		$Type = 'Error';
	}
	elseif($Logo==='N')
	{
		$Type = 'Notice';
	}
	else
	{
		$Type = 'Unknown error';
	}

	return $Type;
}

/**
 * 本函数用于设置错误信息
 */
function TextDB_SetErrorCode($Code,$Info=NULL,$FunName=NULL)
{
	$GLOBALS["TextDB_Error_Code"] = $Code;
	$GLOBALS["TextDB_Error_Info"] = $Info;
	
	/* 检测是否输出错误信息 */
	if(_TextDB_ErrorMShow_===true)
	{
		echo TextDB_Error($Code,$FunName);
	}
	
	/* 检测是否符合中止条件 */
	if(_TextDB_ErrorAOver_===true || (_TextDB_ErrorROver_===true && TextDB_GetErrorType($Code)==='Error'))
	{
		die();
	}
}

/**
 * 本函数用于设置错误信息
 */
function TextDB_Error($Code=NULL,$FunName=NULL)
{
	empty($Code) && $Code=$GLOBALS["TextDB_Error_Code"];
	if(!empty($Code))
	{
		/* 获取错误信息 */
		$Code = strtoupper($Code);
		if(isset($GLOBALS['TextDB_Error'][$Code]))
		{
			$EStr = $GLOBALS['TextDB_Error'][$Code];
		}
		else
		{
			$EStr = $GLOBALS['TextDB_Error']['0_0000'];
		}
		
		/* 错误内容 */
		$Info = $GLOBALS["TextDB_Error_Info"];

		$EInfo = "<br />\n<b><font size='5'>TextDB ".TextDB_GetErrorType($Code)."</font></b><br />\n";
		if(_TextDB_ErrorMSFun_===true)
		{
			$EInfo .= "函数名称： ".$FunName."<br />\n";
		}
		if(_TextDB_ErrorMType_==='ALL' || _TextDB_ErrorMType_==='N')
		{
			$EInfo .= "错误代号： ".$Code."<br />\n";
		}
		if(_TextDB_ErrorMType_==='ALL' || _TextDB_ErrorMType_==='S')
		{
			$EInfo .= "错误描述： ".$EStr."<br />\n";
		}
		if((_TextDB_ErrorMType_==='ALL' || _TextDB_ErrorMType_==='I') && !empty($Info))
		{
			$EInfo .= "错误信息： ".$Info."<br />\n";
		}

		return $EInfo;
	}
	else
	{
		return false;
	}
}

/**
 * 本函数用于设置错误代号
 */
function TextDB_Errno()
{
	return "<br />\nTextDB Error #".$GLOBALS["TextDB_Error_Code"]."<br />\n";
}

/**
 * 建立一个新的访问标识
 * 用于区别资源调用
 */
function TextDB_New_LRL()
{
	$GLOBALS["TextDB_Link_Rand_Logo"]  = md5(uniqid(rand()+uniqid()));
}

/**
 * 本函数用于检测TextDB是否启动
 */
function TextDB_CheckInit()
{
	/* 检测初始化变量是否有效 */
	if(isset($GLOBALS["TextDB_Init"]) && $GLOBALS["TextDB_Init"]===true)
	{
		return true;
	}
	else
	{
		return false;
	}
}

/**
 * 启动TextDB
 * 检测数据库等
 */
function TextDB_Init()
{
	/* 检测数据库是否启动 */
	if(TextDB_CheckInit())
	{
		TextDB_SetErrorCode('W_0000',NULL,'TextDB_Init');
		return false;
	}

	/* 检测数据库存放路径是否存在 */
	if(!file_exists(_TextDB_SQLPath_) || !is_dir(_TextDB_SQLPath_))
	{
		/* 尝试建立目录，不存在则返回假 */
		if(!mkdir(_TextDB_SQLPath_,0777))
		{
			TextDB_SetErrorCode('E_0000',_TextDB_SQLPath_,'TextDB_Init');
			return false;
		}
	}

	/* 下面部分用于初始化记录 */
	/* 本变量如果设置为true则表示TEXTDB已经启动 */
	$GLOBALS["TextDB_Init"] = true;

	/* 本变量 用于记录数据库链接资源ID */
	$GLOBALS["TextDB_Link_DB"] = array();

	/* 本变量用于记录数据库，本次操作的错误ID */
	$GLOBALS["TextDB_Error_Code"] = '0_0000';
	$GLOBALS["TextDB_Error_Info"] = '';

	/* 本变量用于记录数据库尝试解锁次数 */
	$GLOBALS["TextDB_LLC_NOW"] = 0;

	/* 本变量用于记录当前链接资源ID */
	$GLOBALS["TextDB_Link_DB_ID_Save"] = false;

	/* 本变量用于记录下一个链接资源ID */
	$GLOBALS["TextDB_Link_DB_ID_Next"] = 1;

	/* 用于生成本次链接的唯一标识 */
	TextDB_New_LRL();

	/* 下面部分用于初始化系统结构 */
	/* 检测系统库是否存在 */
	if(!TextDB_CheckDB(_TextDB_SysDB_System_))
	{
		/* 建立系统库 */
		TextDB_CreateDB(_TextDB_SysDB_System_);
	}

	/* 链接系统库，并获取链接ID */
	$TextDB_Link_DB_ID = TextDB_ConnectDB(_TextDB_SysDB_System_);

	/* 尝试创建可用数据库清单 */
	TextDB_CreateTable(_TextDB_SysTBL_DataBaseList_,_TextDB_SysTBLF_DataBaseList_,0,NULL,$TextDB_Link_DB_ID);

	/* 尝试创建回收库清单 */
	TextDB_CreateTable(_TextDB_SysTBL_BakDataBaseList_,_TextDB_SysTBLF_BakDataBaseList_,0,NULL,$TextDB_Link_DB_ID);

	/* 尝试创建数据库总清单 */
	TextDB_CreateTable(_TextDB_SysTBL_AllTableList_,_TextDB_SysTBLF_AllTableList_,0,NULL,$TextDB_Link_DB_ID);

	/* 尝试创建用户库 */
	if(TextDB_CreateTable(_TextDB_SysTBL_UserInfo_,_TextDB_SysTBLF_UserInfo_,0,NULL,$TextDB_Link_DB_ID))
	{
		/* 设置初始用户 */
		TextDB_InsertData(_TextDB_SysTBL_UserInfo_,_TextDB_SysTBLF_UserInfo_,explode(",",_TextDB_SysTBLD_UserInfo_),$TextDB_Link_DB_ID);
	}

	/* 关闭链接 */
	TextDB_ConnectClose($TextDB_Link_DB_ID);

	return true;
}

/**
 * 运行 TDL(Text Database Language)
 */
function TextDB_RunTDL($TDLStr)
{
	if(!TextDB_CheckInit())
	{
		return false;
	}

	eval(stripslashes($TDLStr));
	return true;
}

/**
 * 本函数用于建立资源标识
 */
function TextDB_CreateResourceLogo($TextDB_Link_DB_ID)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CreateResourceLogo');
		return false;
	}

	if($TextDB_Link_DB_ID!=='')
	{
		return _TextDB_ResourceLogo_.$TextDB_Link_DB_ID;
	}
	else
	{
		TextDB_SetErrorCode('E_1000',NULL,'TextDB_CreateResourceLogo');
		return false;
	}
}

/**
 * 本函数用于检测连接资源是否合法
 */
function TextDB_CheckResourceLogo($ResourceLogo)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CheckResourceLogo');
		return false;
	}

	if(empty($ResourceLogo))
	{
		TextDB_SetErrorCode('E_1001',NULL,'TextDB_CheckResourceLogo');
		return false;
	}

	/* 检测 TextDB_Link_DB_ID 是否合法 */
	if(substr($ResourceLogo,0,_TextDB_ResourceLogo_Size_)!==_TextDB_ResourceLogo_)
	{

		/* 数据库链接资源不合法 */
		TextDB_SetErrorCode('E_1002',$ResourceLogo,'TextDB_CheckResourceLogo');
		return false;
	}
	else
	{
		return true;
	}

}

/**
 * 本函数用于将资源标识转化成ID
 */
function TextDB_ResourceLogoToID($ResourceLogo)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_ResourceLogoToID');
		return false;
	}

	/* 检测资源是否合法 */
	if(!TextDB_CheckResourceLogo($ResourceLogo))
	{
		return false;
	}

	return str_replace(_TextDB_ResourceLogo_,'',$ResourceLogo);
}

/**
 * 检测数据库当前资源是否存在，并返回资源
 */
function TextDB_GetLinkDBIDSave()
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_GetLinkDBIDSave');
		return false;
	}

	if(isset($GLOBALS["TextDB_Link_DB_ID_Save"]) && $GLOBALS["TextDB_Link_DB_ID_Save"]!==false)
	{
		return TextDB_CreateResourceLogo($GLOBALS["TextDB_Link_DB_ID_Save"]);
	}
	else
	{
		return false;
	}
}

/**
 * 本函数用于检测，并返回正确有效的TextDB_Link_DB_ID
 */
function TextDB_GetLinkDBID($TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_GetLinkDBIDSave');
		return false;
	}

	/* 若变量 $TextDB_Link_DB_ID 为空则返回最后一次链接资源 */
	if(empty($TextDB_Link_DB_ID))
	{
		if(TextDB_GetLinkDBIDSave()!==false)
		{
			/* 设置最后的资源 */
			$TextDB_Link_DB_ID = TextDB_CreateResourceLogo($GLOBALS["TextDB_Link_DB_ID_Save"]);
		}
		else
		{
			/* 还没有链接数据库 */
			TextDB_SetErrorCode('E_1003',NULL,'TextDB_GetLinkDBID');
			return false;
		}
	}

	/* 检测连接资源是否合法 */
	if(!TextDB_CheckResourceLogo($TextDB_Link_DB_ID))
	{
		TextDB_SetErrorCode('E_1002',$TextDB_Link_DB_ID,'TextDB_GetLinkDBID');
		return false;
	}

	/* 若变量 $TextDB_Link_DB_ID 则返回资源信息 */
	if(isset($GLOBALS["TextDB_Link_DB"][$TextDB_Link_DB_ID]))
	{
		return $TextDB_Link_DB_ID;
	}
	else
	{
		/* 数据库链接资源不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_GetLinkDBID');
		return false;
	}
}

/**
 * 本函数用于检测，TextDB_Link_DB_ID 是否属于最后一个连接资源
 */
function TextDB_ISLinkDBIDSave($TextDB_Link_DB_ID)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_ISLinkDBIDSave');
		return false;
	}

	/* 检测连接资源是否合法 */
	if(!TextDB_CheckResourceLogo($TextDB_Link_DB_ID))
	{
		TextDB_SetErrorCode('E_1002',$TextDB_Link_DB_ID,'TextDB_ISLinkDBIDSave');
		return false;
	}

	/* 检测是否为最后一个资源 */
	if($TextDB_Link_DB_ID===TextDB_GetLinkDBIDSave())
	{
		return true;
	}
	else
	{
		return false;
	}
}

/**
 * 本函数用于检测，并返回有效的TextDB_Link_DB_Name
 */
function TextDB_GetLinkDBName($TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_GetLinkDBName');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('W_1001',$TextDB_Link_DB_ID,'TextDB_GetLinkDBName');
		return false;
	}

	return $GLOBALS["TextDB_Link_DB"][$TextDB_Link_DB_ID];
}

/**
 * 检测数据库是否已经连接
 */
function TextDB_CheckDBInLinkDB($DBName)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CheckDBInLinkDB');
		return false;
	}

	if(!isset($GLOBALS["TextDB_Link_DB"]))
	{
		TextDB_SetErrorCode('E_0002',NULL,'TextDB_CheckDBInLinkDB');
		return false;
	}

	if(!array_search($DBName,$GLOBALS["TextDB_Link_DB"]))
	{
		TextDB_SetErrorCode('N_1000',$DBName,'TextDB_CheckDBInLinkDB');
		return false;
	}

	return true;
}

/**
 * 替换已经连接资源的库名称
 */
function TextDB_ChangeLinkDBName($DBName,$NewDBName)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CheckDBInLinkDB');
		return false;
	}

	if(empty($DBName))
	{
		TextDB_SetErrorCode('E_1005',NULL,'TextDB_CheckDBInLinkDB');
		return false;
	}

	if(empty($NewDBName))
	{
		TextDB_SetErrorCode('E_1006',NULL,'TextDB_CheckDBInLinkDB');
		return false;
	}

	if(!isset($GLOBALS["TextDB_Link_DB"]))
	{
		TextDB_SetErrorCode('E_0002',NULL,'TextDB_CheckDBInLinkDB');
		return false;
	}

	if(str_replace($DBName,$NewDBName,$GLOBALS["TextDB_Link_DB"]))
	{
		return true;
	}
	else
	{
		TextDB_SetErrorCode('E_1007',$DBName.' -> '.$NewDBName,'TextDB_CheckDBInLinkDB');
		return false;
	}
}

/**
 * 本函数用于关闭数据库，结束所有操作。
 */
function TextDB_Close()
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CheckDBInLinkDB');
		return false;
	}

	/* 清除设置 */
	unset($GLOBALS["TextDB_Init"],$GLOBALS["TextDB_Link_DB"],$GLOBALS["TextDB_Error_Code"],$GLOBALS["TextDB_Login_ID"],$GLOBALS["TextDB_Link_DB_ID_Next"],$GLOBALS["TextDB_Link_DB_ID_Save"]);
	return true;
}

/**
 * 连接数据库返回链接资源
 */
function TextDB_ConnectDB($DBName)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_ConnectDB');
		return false;
	}

	if(empty($DBName))
	{
		TextDB_SetErrorCode('E_1005',NULL,'TextDB_ConnectDB');
		return false;
	}

	/* 检测数据库名称是否合法 */
	if(TextDB_CheckStr($DBName,_TextDB_SysFilter_DBN_))
	{
		TextDB_SetErrorCode('W_1002',$DBName,'TextDB_ConnectDB');
		return false;
	}

	/* 备份数据库名称，并设置数据库可用路径以及回收路径 */
	$TempDBName = $DBName;
	$DBName     = _TextDB_SQLPath_."/_".$TempDBName;
	$BakDBName  = _TextDB_SQLPath_."/_".$TempDBName._TextDB_Bak_;

	/* 设置数据库资源名称 */
	$TextDB_Link_DB_ID = TextDB_CreateResourceLogo($GLOBALS["TextDB_Link_DB_ID_Next"]);

	/* 检测数据库类别 */
	if(file_exists($DBName) && is_dir($DBName))
	{
		/* 记录数据库资源/可用库 */
		$GLOBALS["TextDB_Link_DB"][$TextDB_Link_DB_ID] = $TempDBName;
	}
	elseif(file_exists($BakDBName) && is_dir($BakDBName))
	{
		/* 记录数据库资源/回收库 */
		TextDB_SetErrorCode('W_1003',$BakDBName,'TextDB_ConnectDB');
		return false;
	}
	else
	{
		TextDB_SetErrorCode('W_1004',$TempDBName,'TextDB_ConnectDB');
		return false;
	}

	/* 记录最后链接ID,并延长记录 */
	$GLOBALS["TextDB_Link_DB_ID_Save"] = $GLOBALS["TextDB_Link_DB_ID_Next"];
	$GLOBALS["TextDB_Link_DB_ID_Next"]++;

	return $TextDB_Link_DB_ID;
}

/**
 * 关闭数据库连接
 **/
function TextDB_ConnectClose($TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_ConnectClose');
		return false;
	}

	/* 检测并获取有效资源 */
	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);

	if(!$TextDB_Link_DB_ID)
	{
		TextDB_SetErrorCode('W_1001',$TextDB_Link_DB_ID,'TextDB_ConnectClose');
		return false;
	}

	/* 如果删除数据库连接属于最后链接 */
	$IS_LASTLINK_DB_ID = false;
	if(TextDB_ISLinkDBIDSave($TextDB_Link_DB_ID))
	{
		$IS_LASTLINK_DB_ID = true;
	}
	
	/* 删除链接资源 */
	unset($GLOBALS["TextDB_Link_DB"][$TextDB_Link_DB_ID]);

	/* 分配最后一个指针 */
	if($IS_LASTLINK_DB_ID)
	{
		if(!empty($GLOBALS["TextDB_Link_DB"]))
		{
			/* 重置记录资源ID */
			end($GLOBALS["TextDB_Link_DB"]);
			$TextDB_Link_DB_ID = key($GLOBALS["TextDB_Link_DB"]);
			$GLOBALS["TextDB_Link_DB_ID_Save"] = TextDB_ResourceLogoToID($TextDB_Link_DB_ID);
			reset($GLOBALS["TextDB_Link_DB"]);
			if(!$GLOBALS["TextDB_Link_DB_ID_Save"])
			{
				return false;
			}
		}
		else
		{
			return true;
		}
	}
	return true;
}

/**
 * 检测数据库是否存在
 */
function TextDB_CheckDB($DBName)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CheckDB');
		return false;
	}

	if(empty($DBName))
	{
		TextDB_SetErrorCode('E_1005',NULL,'TextDB_CheckDB');
		return false;
	}

	/* 检测数据库名称是否符合要求 */
	if(TextDB_CheckStr($DBName,_TextDB_SysFilter_DBN_))
	{
		TextDB_SetErrorCode('W_1002',$DBName,'TextDB_CheckDB');
		return false;
	}

	$TempDBName = $DBName;
	$DBName     = _TextDB_SQLPath_."/_".$TempDBName;
	$BakDBName  = _TextDB_SQLPath_."/_".$TempDBName._TextDB_Bak_;

	if(!file_exists($DBName) && !is_dir($DBName))
	{
		if(file_exists($BakDBName) && is_dir($BakDBName))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return true;
	}
}

//清空 [可用库]
function _CleanDB($DBName)
{
	$TextDB_Link_DB_ID = TextDB_ConnectDB($DBName);
	$tablelist=TextDB_SearchData(_TextDB_SysTBL_TableList_,NULL,NULL,NULL,NULL,NULL,NULL,$TextDB_Link_DB_ID);
	for($dbSN=0;$dbSN<sizeof($tablelist);$dbSN++)
	{
		TextDB_DeleteTable($tablelist[$dbSN]["表名"],true,$TextDB_Link_DB_ID);
	}
	TextDB_ConnectClose($TextDB_Link_DB_ID);
}

//清空 [回收库]
function _CleanBakDB($DBName)
{
	$TextDB_Link_DB_ID = TextDB_ConnectDB($DBName);
	$baktablelist=TextDB_SearchData(_TextDB_SysTBL_BakTableList_,NULL,NULL,NULL,NULL,NULL,NULL,$TextDB_Link_DB_ID);
	for($dbSN=0;$dbSN<sizeof($baktablelist);$dbSN++)
	{
		TextDB_DeleteTable($baktablelist[$dbSN]["表名"],true,$TextDB_Link_DB_ID);
	}
	TextDB_ConnectClose($TextDB_Link_DB_ID);
}

/**
 * 建立数据库
 */
function TextDB_CreateDB($DBName,$DBOpen=0)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CreateDB');
		return false;
	}

	if(empty($DBName))
	{
		TextDB_SetErrorCode('E_1005',NULL,'TextDB_CreateDB');
		return false;
	}

	/* 检测数据库名称是否符合要求 */
	if(TextDB_CheckStr($DBName,_TextDB_SysFilter_DBN_))
	{
		TextDB_SetErrorCode('W_1002',$DBName,'TextDB_CreateDB');
		return false;
	}

	$TempDBName = $DBName;
	$DBName     = _TextDB_SQLPath_."/_".$DBName;
	$BakDBName  = _TextDB_SQLPath_."/_".$TempDBName._TextDB_Bak_;

	if(file_exists($DBName) && is_dir($DBName))
	{
		TextDB_SetErrorCode('W_1005',$DBName,'TextDB_CreateDB');
		return false;
	}
	
	if(file_exists($BakDBName) && is_dir($BakDBName))
	{
		TextDB_SetErrorCode('W_1005',$BakDBName,'TextDB_CreateDB');
		return false;
	}

	/* 尝试建立数据库 */
	if(!mkdir($DBName,0777))
	{
		TextDB_SetErrorCode('E_1008',$DBName,'TextDB_CreateDB');
		return false;
	}

	/* 增加 新增库 数据表 清单 */
	$TextDB_Link_DB_ID = TextDB_ConnectDB($TempDBName);
	TextDB_CreateTable(_TextDB_SysTBL_TableList_,_TextDB_SysTBLF_TableList_,0,NULL,$TextDB_Link_DB_ID);
	TextDB_CreateTable(_TextDB_SysTBL_BakTableList_,_TextDB_SysTBLF_BakTableList_,0,NULL,$TextDB_Link_DB_ID);
	TextDB_CreateTable(_TextDB_SysTBL_DBTableList_,_TextDB_SysTBLF_DBTableList_,0,NULL,$TextDB_Link_DB_ID);
	TextDB_ConnectClose($TextDB_Link_DB_ID);

	if(!TextDB_CheckStr($TempDBName,_TextDB_SysDBSN_,1))
	{
		/* 增加 新增库 记录 */
		$TextDB_Link_DB_ID = TextDB_ConnectDB(_TextDB_SysDB_System_);
		$DBInfo = array($TempDBName,_TextDB_InitUserName_,_TextDB_InitUserName_,$DBOpen,0,0,date("Y-m-d H:i:s"),'0000-00-00 00:00:00','0000-00-00 00:00:00');
		TextDB_InsertData(_TextDB_SysTBL_DataBaseList_,_TextDB_SysTBLF_DataBaseList_,$DBInfo,$TextDB_Link_DB_ID);
		TextDB_ConnectClose($TextDB_Link_DB_ID);
	}

	return true;
}

/**
 * 删除数据库
 */
function TextDB_DeleteDB($DBName,$RM=1)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_DeleteDB');
		return false;
	}

	if(empty($DBName))
	{
		TextDB_SetErrorCode('E_1005',NULL,'TextDB_DeleteDB');
		return false;
	}

	/* 检测数据库名称是否符合要求 */
	if(TextDB_CheckStr($DBName,_TextDB_SysFilter_DBN_))
	{
		TextDB_SetErrorCode('W_1002',$DBName,'TextDB_DeleteDB');
		return false;
	}

	$TempDBName = $DBName;
	$DBName     = _TextDB_SQLPath_."/_".$TempDBName;
	$BakDBName  = _TextDB_SQLPath_."/_".$TempDBName._TextDB_Bak_;

	/* 检测 是否为系统库 */
	if(TextDB_CheckStr($TempDBName,_TextDB_SysDBSN_,1))
	{
		TextDB_SetErrorCode('W_1006',$TempDBName,'TextDB_DeleteDB');
		return false;
	}

	$is_bakdb=false;
	if(!file_exists($DBName) && !is_dir($DBName))
	{
		if(file_exists($BakDBName) && is_dir($BakDBName))
		{
			$DBName = $BakDBName;
			$is_bakdb=true;
		}
		else
		{
			TextDB_SetErrorCode('W_1004',$TempDBName,'TextDB_DeleteDB');
			return false;
		}
	}
	$TextDB_Link_DB_ID = TextDB_ConnectDB(_TextDB_SysDB_System_);
	$DBInfo = TextDB_SearchData(_TextDB_SysTBL_DataBaseList_,"库名",$TempDBName,"asc",NULL,NULL,NULL,$TextDB_Link_DB_ID);
	if($DBInfo===false)
	{
		TextDB_SetErrorCode('E_1009',$TempDBName,'TextDB_DeleteDB');
		return false;
	}
	$DBInfo = $DBInfo[0];

	/* 数据库是否真正删除 */
	if($RM)
	{
		_CleanDB($TempDBName);
		_CleanBakDB($TempDBName);

		$mydirectory = opendir($DBName);
		while($entryname=readdir($mydirectory))
		{
			switch (true)
			{
				case ($entryname=="."):
					break;
				case ($entryname==".."):
					break;
				case (is_dir($DBName."/".$entryname)):
					TextDB_RMDIR($DBName."/".$entryname);
					break;
				default:
					unlink($DBName."/".$entryname);
					break;
			}
		}
		closedir($mydirectory);
		rmdir($DBName);
		if($bak)
		{
			TextDB_DeleteData(_TextDB_SysTBL_BakDataBaseList_,"库名",$DBInfo['库名'],$TextDB_Link_DB_ID);
		}

		_ChangeTableSav('A',$TempDBName);
	}
	else
	{
		if($is_bakdb)
		{
			TextDB_SetErrorCode('W_1007',$TempDBName,'TextDB_DeleteDB');
			return false;
		}
		
		/* 回收设置 */
		if(rename($DBName,$BakDBName))
		{
			$DBInfoData = array($DBInfo['库名'],$DBInfo['建立者'],$DBInfo['拥有者'],$DBInfo['开放'],$DBInfo['表数'],$DBInfo['行数'],$DBInfo['建立日期'],date('Y-m-d H:i:s'),$DBInfo['还原日期']);
			TextDB_InsertData(_TextDB_SysTBL_BakDataBaseList_,_TextDB_SysTBLF_BakDataBaseList_,$DBInfoData,$TextDB_Link_DB_ID);
		}
		else
		{
			TextDB_ConnectClose($TextDB_Link_DB_ID);
			TextDB_SetErrorCode('E_1010',$DBName.' -> '.$BakDBName,'TextDB_DeleteDB');
			return false;
		}
	}

	/* 删除 数据库 记录 */
	TextDB_DeleteData(_TextDB_SysTBL_DataBaseList_,"库名",$DBInfo['库名'],$TextDB_Link_DB_ID);
	TextDB_ConnectClose($TextDB_Link_DB_ID);

	return true;
}

/**
 * 还原被回收的数据库
 */
function TextDB_ResumeDB($DBName)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_ResumeDB');
		return false;
	}

	if(empty($DBName))
	{
		TextDB_SetErrorCode('E_1005',NULL,'TextDB_ResumeDB');
		return false;
	}

	/* 检测数据库名称是否符合要求 */
	if(TextDB_CheckStr($DBName,_TextDB_SysFilter_DBN_))
	{
		TextDB_SetErrorCode('W_1002',$DBName,'TextDB_ResumeDB');
		return false;
	}

	$TempDBName = $DBName;
	$DBName     = _TextDB_SQLPath_."/_".$TempDBName._TextDB_Bak_;
	$NewDBName  = _TextDB_SQLPath_."/_".$TempDBName;

	/* 检测 是否为系统库 */
	if(TextDB_CheckStr($TempDBName,_TextDB_SysDBSN_,1))
	{
		TextDB_SetErrorCode('W_1006',$TempDBName,'TextDB_ResumeDB');
		return false;
	}

	if(!file_exists($DBName) and !is_dir($DBName))
	{
		TextDB_SetErrorCode('W_1004',$DBName,'TextDB_ResumeDB');
		return false;
	}

	if(file_exists($NewDBName) and is_dir($NewDBName))
	{
		TextDB_SetErrorCode('W_1005',$NewDBName,'TextDB_ResumeDB');
		return false;
	}

	/* 还原库 */
	if(rename($DBName,$NewDBName))
	{
		$TextDB_Link_DB_ID = TextDB_ConnectDB(_TextDB_SysDB_System_);
		$DBInfo = TextDB_SearchData(_TextDB_SysTBL_BakDataBaseList_,"库名",$TempDBName,"asc",NULL,NULL,NULL,$TextDB_Link_DB_ID);
		if($DBInfo===false)
		{
			TextDB_SetErrorCode('E_1009',$TempDBName,'TextDB_ResumeDB');
			return false;
		}
		$DBInfo = $DBInfo[0];
		$DBInfoData = array($DBInfo['库名'],$DBInfo['建立者'],$DBInfo['拥有者'],$DBInfo['开放'],$DBInfo['表数'],$DBInfo['行数'],$DBInfo['建立日期'],$DBInfo['回收日期'],date('Y-m-d H:i:s'));
		TextDB_InsertData(_TextDB_SysTBL_DataBaseList_,_TextDB_SysTBLF_DataBaseList_,$DBInfoData,$TextDB_Link_DB_ID);
		TextDB_DeleteData(_TextDB_SysTBL_BakDataBaseList_,"库名",$DBInfo['库名'],$TextDB_Link_DB_ID);
		TextDB_ConnectClose($TextDB_Link_DB_ID);

		return true;
	}
	else
	{
		TextDB_SetErrorCode('E_1011',$DBName.' -> '.$NewDBName,'TextDB_ResumeDB');
		return false;
	}
}

/**
 * 修改数据库名
 */
function TextDB_ChangeDB($DBName,$NewDBName,$NewDBOpen)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_ChangeDB');
		return false;
	}

	if(empty($DBName))
	{
		TextDB_SetErrorCode('E_1005',NULL,'TextDB_ChangeDB');
		return false;
	}

	if(empty($NewDBName))
	{
		TextDB_SetErrorCode('E_1006',NULL,'TextDB_ChangeDB');
		return false;
	}

	/* 检测数据库名称是否符合要求 */
	if(TextDB_CheckStr($DBName,_TextDB_SysFilter_DBN_))
	{
		TextDB_SetErrorCode('W_1002',$DBName,'TextDB_ChangeDB');
		return false;
	}

	/* 检测新数据库名称是否符合要求 */
	if(TextDB_CheckStr($NewDBName,_TextDB_SysFilter_DBN_))
	{
		TextDB_SetErrorCode('W_1002',$NewDBName,'TextDB_ChangeDB');
		return false;
	}

	$TempDBName    = $DBName;
	$TempNewDBName = $NewDBName;
	$DBName        = _TextDB_SQLPath_."/_".$TempDBName;
	$NewDBName     = _TextDB_SQLPath_."/_".$TempNewDBName;

	/* 检测 是否为系统库 */
	if(TextDB_CheckStr($TempDBName,_TextDB_SysDBSN_,1))
	{
		TextDB_SetErrorCode('W_1006',$TempDBName,'TextDB_ChangeDB');
		return false;
	}

	if(TextDB_CheckStr($TempNewDBName,_TextDB_SysDBSN_,1))
	{
		TextDB_SetErrorCode('W_1006',$TempNewDBName,'TextDB_ChangeDB');
		return false;
	}

	/* 检测 修改库是否存在，新建库是否存在 */
	if(!file_exists($DBName) and !is_dir($DBName))
	{
		TextDB_SetErrorCode('W_1004',$DBName,'TextDB_ChangeDB');
		return false;
	}

	/* 检测是否存在连接资源中 */
	if(TextDB_CheckDBInLinkDB($TempDBName))
	{
		/* 替换连接资源中的数据库名称 */
		if(!TextDB_ChangeLinkDBName($TempDBName,$TempNewDBName))
		{
			TextDB_SetErrorCode('E_1007',$TempDBName.' -> '.$TempNewDBName,'TextDB_ChangeDB');
			return false;
		}
	}

	if($DBName!=$NewDBName)
	{
		if(!rename($DBName,$NewDBName))
		{
			TextDB_SetErrorCode('E_1012',$DBName.' -> '.$NewDBName,'TextDB_ChangeDB');
			return false;
		}
	}

	$Fields[] = "库名";
	$Fields[] = "开放";

	$DB[]     = $TempDBName;

	$NewDB[]  = $TempNewDBName;
	$NewDB[]  = $NewDBOpen;

	/* 更新系统数据库记录 */
	$TextDB_Link_DB_ID = TextDB_ConnectDB(_TextDB_SysDB_System_);
	TextDB_ChangeData(_TextDB_SysTBL_DataBaseList_,$Fields[0],$DB[0],$Fields,$NewDB,$TextDB_Link_DB_ID);
	TextDB_ConnectClose($TextDB_Link_DB_ID);

	/* 更新库中表对应记录 */
	$TextDB_Link_DB_ID = TextDB_ConnectDB($TempNewDBName);
	TextDB_ChangeData(_TextDB_SysTBL_TableList_,"所属库",$TempDBName,"所属库",$TempNewDBName,$TextDB_Link_DB_ID);
	TextDB_ChangeData(_TextDB_SysTBL_BakTableList_,"所属库",$TempDBName,"所属库",$TempNewDBName,$TextDB_Link_DB_ID);
	_ChangeTableSav('T',array($TempDBName,$TempNewDBName),$TextDB_Link_DB_ID);
	TextDB_ConnectClose($TextDB_Link_DB_ID);
	return true;
}

/**
 * 复制数据库
 */
function TextDB_CopyDB($DBName,$NewDBName)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CopyDB');
		return false;
	}

	if(empty($DBName))
	{
		TextDB_SetErrorCode('E_1005',NULL,'TextDB_CopyDB');
		return false;
	}

	if(empty($NewDBName))
	{
		TextDB_SetErrorCode('E_1006',NULL,'TextDB_CopyDB');
		return false;
	}

	/* 检测数据库名称是否符合要求 */
	if(TextDB_CheckStr($DBName,_TextDB_SysFilter_DBN_))
	{
		TextDB_SetErrorCode('W_1002',$DBName,'TextDB_CopyDB');
		return false;
	}

	/* 检测新数据库名称是否符合要求 */
	if(TextDB_CheckStr($NewDBName,_TextDB_SysFilter_DBN_))
	{
		TextDB_SetErrorCode('W_1002',$NewDBName,'TextDB_CopyDB');
		return false;
	}

	$TempDBName    = $DBName;
	$TempNewDBName = $NewDBName;
	$DBName        = _TextDB_SQLPath_."/_".$DBName;
	$NewDBName     = _TextDB_SQLPath_."/_".$NewDBName;

	/* 检测 是否为系统库 */
	if(TextDB_CheckStr($TempDBName,_TextDB_SysDBSN_,1))
	{
		TextDB_SetErrorCode('W_1006',$TempDBName,'TextDB_CopyDB');
		return false;
	}

	if(TextDB_CheckStr($TempNewDBName,_TextDB_SysDBSN_,1))
	{
		TextDB_SetErrorCode('W_1006',$TempNewDBName,'TextDB_CopyDB');
		return false;
	}

	if(!file_exists($DBName) && !is_dir($DBName))
	{
		TextDB_SetErrorCode('W_1004',$DBName,'TextDB_CopyDB');
		return false;
	}

	if(file_exists($NewDBName) && is_dir($NewDBName))
	{
		TextDB_SetErrorCode('W_1005',$NewDBName,'TextDB_CopyDB');
		return false;
	}

	//获取原 数据库 信息
	$TextDB_Link_DB_ID = TextDB_ConnectDB(_TextDB_SysDB_System_);
	$dbinfo=TextDB_SearchData(_TextDB_SysTBL_DataBaseList_,"库名",$TempDBName,NULL,NULL,NULL,NULL,$TextDB_Link_DB_ID);
	TextDB_ConnectClose($TextDB_Link_DB_ID);

	//建立新数据库
	TextDB_CreateDB($TempNewDBName,$dbinfo[0]["开放"]);

	$mydirectory = opendir($DBName);
	while($entryname=readdir($mydirectory))
	{
		switch (true)
		{
			case ($entryname=="."):
				break;
			case ($entryname==".."):
				break;
			case (is_dir($DBName."/".$entryname)):
				//建立目录
				mkdir($NewDBName."/".$entryname,0777);
				TextDB_CopyDB($DBName."/".$entryname);
				break;
			default:
				copy($DBName."/".$entryname,$NewDBName."/".$entryname);
				break;
		}
	}
	closedir($mydirectory);

	//更新 库中表对应的库
	$TextDB_Link_DB_ID = TextDB_ConnectDB($TempNewDBName);
	TextDB_ChangeData(_TextDB_SysTBL_TableList_,"所属库",$TempDBName,"所属库",$TempNewDBName,$TextDB_Link_DB_ID);
	TextDB_ChangeData(_TextDB_SysTBL_BakTableList_,"所属库",$TempDBName,"所属库",$TempNewDBName,$TextDB_Link_DB_ID);
	_ChangeTableSav('F',array($TempDBName,$TempNewDBName),$TextDB_Link_DB_ID);
	TextDB_ConnectClose($TextDB_Link_DB_ID);

	return true;
}

/**
 * 清空数据库
 * 默认只清除可用库
 */
function TextDB_CleanDB($DBName,$CleanMode=1)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CleanDB');
		return false;
	}

	if(empty($DBName))
	{
		TextDB_SetErrorCode('E_1005',NULL,'TextDB_CleanDB');
		return false;
	}

	if($CleanMode<0 || $CleanMode>2)
	{
		TextDB_SetErrorCode('W_1008',$CleanMode.'<>0~2','TextDB_CleanDB');
		return false;
	}

	//检测数据库名称是否符合要求
	if(TextDB_CheckStr($DBName,_TextDB_SysFilter_DBN_))
	{
		TextDB_SetErrorCode('W_1002',$DBName,'TextDB_CleanDB');
		return false;
	}

	$TempDBName = $DBName;
	$DBName     = _TextDB_SQLPath_."/_".$DBName;

	//检测 是否为系统库
	if(TextDB_CheckStr($TempDBName,_TextDB_SysDBSN_,1))
	{
		TextDB_SetErrorCode('W_1006',$TempDBName,'TextDB_CleanDB');
		return false;
	}

	if(!file_exists($DBName) && !is_dir($DBName))
	{
		TextDB_SetErrorCode('W_1004',$DBName,'TextDB_CleanDB');
		return false;
	}

	switch($CleanMode)
	{
		case 0:
			_CleanDB($TempDBName);
			_CleanBakDB($TempDBName);
			break;
		case 1:
			_CleanDB($TempDBName);
			break;
		case 2:
			_CleanBakDB($TempDBName);
			break;
	}
	return true;
}

//检测表是否存在
function TextDB_CheckTable($TableName,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CheckTable');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_CheckTable');
		return false;
	}

	/* 检测数据表名称是否符合要求 */
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_CheckTable');
		return false;
	}

	/* 检测并获取有效资源 */
	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_CheckTable');
		return false;
	}

	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			return true;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_CheckTable');
			return false;
		}
	}
	else
	{
		return true;
	}
}

/**
 * 检测数据表锁定状态
 */
function TextDB_isLockTable($TableName,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_isLockTable');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_isLockTable');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_isLockTable');
		return false;
	}

	$LockFile = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TableName._TextDB_Lock_._TextDB_LTBL_;
	if(file_exists($LockFile) && is_file($LockFile))
	{
		if(_TextDB_LLCount_!==0)
		{
			$GLOBALS["TextDB_LLC_NOW"]++;
			if($GLOBALS["TextDB_LLC_NOW"]>_TextDB_LLCount_)
			{
				$GLOBALS["TextDB_LLC_NOW"] = 0;
				TextDB_SetErrorCode('E_2000',$TableName,'TextDB_isLockTable');
				return false;
			}
		}
		return true;
	}
	else
	{
		$GLOBALS["TextDB_LLC_NOW"] = 0;
		return false;
	}
}

/**
 * 锁定数据表
 */
function TextDB_LockTable($TableName,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_LockTable');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_LockTable');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_LockTable');
		return false;
	}

	set_time_limit(_TextDB_LLMTime_);
	@touch(_TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TableName._TextDB_Lock_._TextDB_LTBL_);
	return true;
}

/**
 * 数据表解锁
 */
function TextDB_UnlockTable($TableName,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_LockTable');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_LockTable');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_LockTable');
		return false;
	}

	@unlink(_TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TableName._TextDB_Lock_._TextDB_LTBL_);
	return true;
}

/**
 * 数据表操作，总记录表变更
 * Type 说明
 * N 创建表插入信息
 * R 修复表,更新信息
 * B 回收表，更新信息
 * D 删除记录
 * C 记录表名称变更
 * I 复制表插入信息
 * T 数据库变更操作,修改所属库信息
 * F 数据库复制操作,插入新复制数据
 * A 数据库删除操作,删除记录信息
 */
function _ChangeTableSav($Type,$TableName,$TextDB_Link_DB_ID=NULL)
{
	$TextDB_SYSDB_Link_ID = TextDB_ConnectDB(_TextDB_SysDB_System_);
	$DBName   = TextDB_GetLinkDBName($TextDB_Link_DB_ID);
	$IS_SYSDB = false;
	if(TextDB_CheckStr($DBName,_TextDB_SysDBSN_,1))
	{
		$IS_SYSDB = true;
	}
	else
	{
		$DBInfo = TextDB_SearchData(_TextDB_SysTBL_DataBaseList_,'库名',$DBName,"asc",NULL,NULL,NULL,$TextDB_SYSDB_Link_ID);
		$DBInfo = $DBInfo[0];
	}

	if($Type==='N' || $Type==='R' || $Type==='B')
	{
		if($Type==='R' || $Type==='N')
		{
			$Stat            = '1';
			$SearchTableName = _TextDB_SysTBL_TableList_;
		}
		else
		{
			$Stat            = '0';
			$SearchTableName = _TextDB_SysTBL_BakTableList_;
		}

		$TableInfo = TextDB_SearchData($SearchTableName,'表名',$TableName,NULL,NULL,NULL,NULL,$TextDB_Link_DB_ID);
		$TableInfo = $TableInfo[0];

		$Info  = array($TableInfo['表名'],$TableInfo['所属库']);
		$Info2 = array($TableInfo['表名'],$TableInfo['建立者'],$TableInfo['所属库'],$TableInfo['开放'],$TableInfo['计数'],$TableInfo['行数'],$TableInfo['建立日期'],$TableInfo['回收日期'],$TableInfo['还原日期'],$Stat);

		if($Type!=='N')
		{
			TextDB_ChangeData(_TextDB_SysTBL_DBTableList_,'表明,所属库',$Info,'表名,建立者,所属库,开放,计数,行数,建立日期,回收日期,还原日期,当前状态',$Info2,$TextDB_Link_DB_ID);
			TextDB_ChangeData(_TextDB_SysTBL_AllTableList_,'表明,所属库',$Info,'表名,建立者,所属库,开放,计数,行数,建立日期,回收日期,还原日期,当前状态',$Info2,$TextDB_SYSDB_Link_ID);
		}
		else
		{
			TextDB_InsertData(_TextDB_SysTBL_DBTableList_,_TextDB_SysTBLF_DBTableList_,$Info2,$TextDB_Link_DB_ID);
			TextDB_InsertData(_TextDB_SysTBL_AllTableList_,_TextDB_SysTBLF_AllTableList_,$Info2,$TextDB_SYSDB_Link_ID);
			
			if(!$IS_SYSDB)
			{
				/* 变更库记录 */
				TextDB_ChangeData(_TextDB_SysTBL_DataBaseList_,'库名',$DBName,'表数',$DBInfo['表数']+1,$TextDB_SYSDB_Link_ID);
			}
		}
	}
	elseif($Type==='D')
	{
		$Info = array($TableName,$DBName);
		TextDB_DeleteData(_TextDB_SysTBL_DBTableList_,'表明,所属库',$Info,$TextDB_Link_DB_ID);
		TextDB_DeleteData(_TextDB_SysTBL_AllTableList_,'表明,所属库',$Info,$TextDB_SYSDB_Link_ID);

		if(!$IS_SYSDB)
		{
			/* 变更库记录 */
			TextDB_ChangeData(_TextDB_SysTBL_DataBaseList_,'库名',$DBName,'表数',$DBInfo['表数']-1,$TextDB_SYSDB_Link_ID);
		}
	}
	elseif($Type==='C')
	{
		$Info  = array($TableName[0],$DBName);
		TextDB_ChangeData(_TextDB_SysTBL_DBTableList_,'表明,所属库',$Info,'表名',$TableName[1],$TextDB_Link_DB_ID);
		TextDB_ChangeData(_TextDB_SysTBL_AllTableList_,'表明,所属库',$Info,'表名',$TableName[1],$TextDB_SYSDB_Link_ID);
	}
	elseif($Type==='I')
	{
		$Info = array($TableName[0],$DBName);

		$TableSavInfo = TextDB_SearchData(_TextDB_SysTBL_DBTableList_,'表明,所属库',$Info,NULL,NULL,NULL,NULL,$TextDB_Link_DB_ID);
		$TableSavInfo = $TableSavInfo[0];
		$TableSavInfoData = array($TableName[1],$TableSavInfo['建立者'],$TableSavInfo['所属库'],$TableSavInfo['开放'],$TableSavInfo['计数'],$TableSavInfo['行数'],$TableSavInfo['建立日期'],$TableSavInfo['回收日期'],$TableSavInfo['还原日期'],$TableSavInfo['当前状态']);
		TextDB_InsertData(_TextDB_SysTBL_DBTableList_,_TextDB_SysTBLF_DBTableList_,$TableSavInfoData,$TextDB_Link_DB_ID);

		$TableSavInfo = TextDB_SearchData(_TextDB_SysTBL_AllTableList_,'表明,所属库',$Info,NULL,NULL,NULL,NULL,$TextDB_SYSDB_Link_ID);
		$TableSavInfo = $TableSavInfo[0];
		$TableSavInfoData = array($TableName[1],$TableSavInfo['建立者'],$TableSavInfo['所属库'],$TableSavInfo['开放'],$TableSavInfo['计数'],$TableSavInfo['行数'],$TableSavInfo['建立日期'],$TableSavInfo['回收日期'],$TableSavInfo['还原日期'],$TableSavInfo['当前状态']);
		TextDB_InsertData(_TextDB_SysTBL_AllTableList_,_TextDB_SysTBLF_AllTableList_,$TableSavInfoData,$TextDB_SYSDB_Link_ID);
	}
	elseif($Type==='T')
	{
		TextDB_ChangeData(_TextDB_SysTBL_DBTableList_,'所属库',$TableName[0],'所属库',$TableName[1],$TextDB_Link_DB_ID);
		TextDB_ChangeData(_TextDB_SysTBL_AllTableList_,'所属库',$TableName[0],'所属库',$TableName[1],$TextDB_SYSDB_Link_ID);
	}
	elseif($Type==='F')
	{
		TextDB_ChangeData(_TextDB_SysTBL_DBTableList_,'所属库',$TableName[0],'所属库',$TableName[1],$TextDB_Link_DB_ID);
		$TableSavInfo = TextDB_SearchData(_TextDB_SysTBL_AllTableList_,'所属库',$TableName[0],NULL,NULL,NULL,NULL,$TextDB_SYSDB_Link_ID);
		if($TableSavInfo!==false)
		{
			foreach($TableSavInfo as $SavInfo)
			{
				$TableSavInfoData = array($SavInfo['表名'],$SavInfo['建立者'],$TableName[1],$SavInfo['开放'],$SavInfo['计数'],$SavInfo['行数'],$SavInfo['建立日期'],$SavInfo['回收日期'],$SavInfo['还原日期'],$SavInfo['当前状态']);
				TextDB_InsertData(_TextDB_SysTBL_AllTableList_,_TextDB_SysTBLF_AllTableList_,$TableSavInfoData,$TextDB_SYSDB_Link_ID);
			}
		}
	}
	elseif($Type==='A')
	{
		TextDB_DeleteData(_TextDB_SysTBL_AllTableList_,'所属库',$TableName,$TextDB_SYSDB_Link_ID);
	}

	TextDB_ConnectClose($TextDB_SYSDB_Link_ID);
	return true;
}

/**
 * 创建数据表
 */
function TextDB_CreateTable($TableName,$Fields,$Open=0,$HeaderInfo=NULL,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CreateTable');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_CreateTable');
		return false;
	}

	if(empty($Fields))
	{
		TextDB_SetErrorCode('W_2005',NULL,'TextDB_CreateTable');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_CreateTable');
		return false;
	}

	/* 检测数据表名称是否符合要求 */
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_CreateTable');
		return false;
	}

	$Fields = TextDB_FilterStr($Fields);

	/* 获取 数据表 名称和所属库 */
	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	/* 检测 是否为系统表 */
	$is_systable = false;
	if(TextDB_CheckStr($TempTableName,_TextDB_SysTBSN_,1))
	{
		$is_systable = true;
	}

	/* 检测表 */
	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(!$is_systable && file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2004',$BakTableName,'TextDB_CreateTable');
			return false;
		}
	}
	else
	{
		TextDB_SetErrorCode('W_2004',$TableName,'TextDB_CreateTable');
		return false;
	}

	/* 建立 数据表 */
	touch($TableName);

	/* 设置 数据表 权限 */
	if(!chmod($TableName,0777))
	{
		TextDB_SetErrorCode('E_2001',$TableName,'TextDB_CreateTable');
		return false;
	}

	/* 如果 字段信息 是 数组 ,就合并 */
	if(is_array($Fields))
	{
		$Fields=implode(",",$Fields);
	}

	/* 过滤 字段信息 中系统冲突字符 */
	$Fields=str_replace(_TextDB_EXStr_,_TextDB_REXStr_,$Fields);

	/* 替换 字段信息 中的 默认分割字段 */
	$Fields=str_replace(",",_TextDB_EXStr_,$Fields);

	/*  检测是否字定义 安全信息,空就自添加 */
	if($HeaderInfo=="")
	{
		$HeaderInfo = "die(\"TextDB "._TextDB_Ver_."\");";
	}

	/* 过滤 安全信息 中系统冲突字符 */
	$HeaderInfo = str_replace("<?php","",$HeaderInfo);
	$HeaderInfo = str_replace("?>","",$HeaderInfo);
	$HeaderInfo = str_replace(_TextDB_EXStr_,_TextDB_REXStr_,$HeaderInfo);

	/* 添加 安全信息 可执行 */
	$HeaderInfo = "<?php ".$HeaderInfo." ?>[自增长]TEXTDB_HID";

	/* 融合 安全信息 和 字段信息 */
	$Fields = $HeaderInfo._TextDB_EXStr_.$Fields._TextDB_EXStr_;

	/* 建立 数据表 */
	TextDB_FileWrite($TableName,"w",$Fields);

	/* 不是系统表则记录到表记录中 */
	if(!$is_systable)
	{
		$TableListData = array($TempTableName,_TextDB_InitUserName_,TextDB_GetLinkDBName($TextDB_Link_DB_ID),$Open,0,0,date("Y-m-d H:i:s"),'0000-00-00 00:00:00','0000-00-00 00:00:00');
		TextDB_InsertData(_TextDB_SysTBL_TableList_,_TextDB_SysTBLF_TableList_,$TableListData,$TextDB_Link_DB_ID);

		_ChangeTableSav('N',$TempTableName,$TextDB_Link_DB_ID);
	}

	return true;
}

/**
 * 数据表删除
 */
function TextDB_DeleteTable($TableName,$RM=true,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_DeleteTable');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_DeleteTable');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_DeleteTable');
		return false;
	}

	/* 检测数据表名称是否符合要求 */
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_DeleteTable');
		return false;
	}

	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	//检测 是否为系统表
	if(TextDB_CheckStr($TempTableName,_TextDB_SysTBSN_,1))
	{
		TextDB_SetErrorCode('W_2006',$TempTableName,'TextDB_DeleteTable');
		return false;
	}

	$is_baktable = false;
	if(!file_exists($TableName) and !is_file($TableName))
	{
		if(file_exists($BakTableName) and is_file($BakTableName))
		{
			$TableName   = $BakTableName;
			$is_baktable = true;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_DeleteTable');
			return false;
		}
	}

	//检测文件是否被锁定
	while(TextDB_isLockTable($TempTableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TempTableName,$TextDB_Link_DB_ID);

	if($RM)
	{
		unlink($TableName);
		if($is_baktable)
		{
			TextDB_DeleteData(_TextDB_SysTBL_BakTableList_,"表名",$TempTableName,$TextDB_Link_DB_ID);
		}
		_ChangeTableSav('D',$TempTableName,$TextDB_Link_DB_ID);
	}
	else
	{
		if(rename($TableName,$BakTableName))
		{
			$TableInfo     = TextDB_SearchData(_TextDB_SysTBL_TableList_,'表名,所属库',array($TempTableName,TextDB_GetLinkDBName($TextDB_Link_DB_ID)),NULL,NULL,NULL,NULL,$TextDB_Link_DB_ID);
			$TableInfo     = $TableInfo[0];
			$TableInfoData = array($TableInfo['表名'],$TableInfo['建立者'],$TableInfo['所属库'],$TableInfo['开放'],$TableInfo['计数'],$TableInfo['行数'],$TableInfo['建立日期'],date('Y-m-d H:i:s'),$TableInfo['还原日期']);
			TextDB_InsertData(_TextDB_SysTBL_BakTableList_,_TextDB_SysTBLF_BakTableList_,$TableInfoData,$TextDB_Link_DB_ID);
			_ChangeTableSav('B',$TempTableName,$TextDB_Link_DB_ID);
		}
		else
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('E_2002',$TableName.' -> '.$BakTableName,'TextDB_DeleteTable');
			return false;
		}
	}
	TextDB_DeleteData(_TextDB_SysTBL_TableList_,"表名",$TempTableName,$TextDB_Link_DB_ID);

	//释放文件
	TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

	return true;
}

/**
 * 还原数据表
 */
function TextDB_ResumeTable($TableName,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_ResumeTable');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_ResumeTable');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_ResumeTable');
		return false;
	}

	/* 检测数据表名称是否符合要求 */
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_ResumeTable');
		return false;
	}

	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	/* 检测 是否为系统表 */
	if(TextDB_CheckStr($TempTableName,_TextDB_SysTBSN_,1))
	{
		TextDB_SetErrorCode('W_2006',$TempTableName,'TextDB_ResumeTable');
		return false;
	}

	if(file_exists($TableName) && is_file($TableName))
	{
		TextDB_SetErrorCode('W_2003',$TableName,'TextDB_ResumeTable');
		return false;
	}

	if(!file_exists($BakTableName) && !is_file($BakTableName))
	{
		TextDB_SetErrorCode('W_2004',$BakTableName,'TextDB_ResumeTable');
		return false;
	}

	/* 检测文件是否被锁定 */
	while(TextDB_isLockTable($TempTableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	/* 锁定数据库 */
	TextDB_LockTable($TempTableName,$TextDB_Link_DB_ID);

	/* 还原数据表 */
	if(rename($BakTableName,$TableName))
	{
		/* 数据表记录操作 */
		$TableInfo     = TextDB_SearchData(_TextDB_SysTBL_BakTableList_,'表名,所属库',array($TempTableName,TextDB_GetLinkDBName($TextDB_Link_DB_ID)),NULL,NULL,NULL,NULL,$TextDB_Link_DB_ID);
		$TableInfo     = $TableInfo[0];
		$TableInfoData = array($TableInfo['表名'],$TableInfo['建立者'],$TableInfo['所属库'],$TableInfo['开放'],$TableInfo['计数'],$TableInfo['行数'],$TableInfo['建立日期'],$TableInfo['回收日期'],date('Y-m-d H:i:s'));
		TextDB_DeleteData(_TextDB_SysTBL_BakTableList_,"表名",$TempTableName,$TextDB_Link_DB_ID);
		TextDB_InsertData(_TextDB_SysTBL_TableList_,_TextDB_SysTBLF_TableList_,$TableInfoData,$TextDB_Link_DB_ID);
		_ChangeTableSav('R',$TempTableName,$TextDB_Link_DB_ID);

		/* 释放文件 */
		TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
		return true;
	}
	else
	{
		/* 释放文件 */
		TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
		TextDB_SetErrorCode('E_2004',$BakTableName.' -> '.$TableName,'TextDB_ResumeTable');
		return false;
	}
}

/**
 * 修改数据表
 **/
function TextDB_ChangeTable($TableName,$TableFields=null,$TableOpen=null,$NewTableName,$NewTableFields=null,$NewTableOpen=null,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_ChangeTable');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_ChangeTable');
		return false;
	}

	if(empty($NewTableName))
	{
		TextDB_SetErrorCode('W_2001',NULL,'TextDB_ChangeTable');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_ChangeTable');
		return false;
	}

	/* 检测数据表名称是否符合要求 */
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_ChangeTable');
		return false;
	}

	/* 检测新数据表名称是否符合要求 */
	if(TextDB_CheckStr($NewTableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$NewTableName,'TextDB_ChangeTable');
		return false;
	}

	$TempTableName    = $TableName;
	$TempNewTableName = $NewTableName;
	$TableName        = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$NewTableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempNewTableName._TextDB_TBL_;

	//检测 是否为系统表
	if(TextDB_CheckStr($TempTableName,_TextDB_SysTBSN2_,1))
	{
		TextDB_SetErrorCode('W_2006',$TempTableName,'TextDB_ChangeTable');
		return false;
	}

	if(TextDB_CheckStr($TempNewTableName,_TextDB_SysTBSN2_,1) || TextDB_CheckStr($TempTableName,_TextDB_SysTBSN_,1) || TextDB_CheckStr($TempNewTableName,_TextDB_SysTBSN_,1))
	{
		TextDB_SetErrorCode('W_2006',$TempNewTableName,'TextDB_ChangeTable');
		return false;
	}

	if(!file_exists($TableName) && !is_file($TableName))
	{
		TextDB_SetErrorCode('W_2003',$TableName,'TextDB_ChangeTable');
		return false;
	}

	/* 检测新数据表是否存在 */
	if($TempTableName!=$TempNewTableName && file_exists($NewTableName) && is_file($NewTableName))
	{
		TextDB_SetErrorCode('W_2004',$NewTableName,'TextDB_ChangeTable');
		return false;
	}

	//检测文件是否被锁定
	while(TextDB_isLockTable($TempTableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TempTableName,$TextDB_Link_DB_ID);

	//修改表名
	if($TableName!=$NewTableName)
	{
		if(!rename($TableName,$NewTableName))
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

			TextDB_SetErrorCode('E_2004',$TableName.' -> '.$NewTableName,'TextDB_ChangeTable');
			return false;
		}
	}

	$Fields[]   = "表名";
	$Fields[]   = "开放";

	$Table[]    = $TempTableName;
	$Table[]    = $TableOpen;

	$NewTable[] = $TempNewTableName;
	$NewTable[] = $NewTableOpen;

	TextDB_ChangeData(_TextDB_SysTBL_TableList_,$Fields,$Table,$Fields,$NewTable,$TextDB_Link_DB_ID);

	_ChangeTableSav('C',array($TempTableName,$TempNewTableName),$TextDB_Link_DB_ID);

	//修改字段
	if($TableFields!=$NewTableFields)
	{
		$NewTableFields = TextDB_FilterStr($NewTableFields);
		_TextDB_UpdateFields($TempNewTableName,$NewTableFields,$TextDB_Link_DB_ID);
	}

	//释放文件
	TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

	return true;
}

/**
 * 复制数据表
 */
function TextDB_CopyTable($TableName,$NewTableName,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CopyTable');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_CopyTable');
		return false;
	}

	if(empty($NewTableName))
	{
		TextDB_SetErrorCode('W_2001',NULL,'TextDB_CopyTable');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_CopyTable');
		return false;
	}

	/* 检测数据表名称是否符合要求 */
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_CopyTable');
		return false;
	}

	/* 检测新数据表名称是否符合要求 */
	if(TextDB_CheckStr($NewTableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$NewTableName,'TextDB_CopyTable');
		return false;
	}

	$TempTableName    = $TableName;
	$TempNewTableName = $NewTableName;
	$TableName        = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$NewTableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempNewTableName._TextDB_TBL_;

	/* 检测 是否为系统表 */
	if(TextDB_CheckStr($TempTableName,_TextDB_SysTBSN_,1))
	{
		TextDB_SetErrorCode('W_2006',$TempTableName,'TextDB_CopyTable');
		return false;
	}

	if(TextDB_CheckStr($TempNewTableName,_TextDB_SysTBSN_,1))
	{
		TextDB_SetErrorCode('W_2006',$TempNewTableName,'TextDB_CopyTable');
		return false;
	}

	/* 检测 数据表是否存在 */
	if(!file_exists($TableName) && !is_file($TableName))
	{
		TextDB_SetErrorCode('W_2003',$TableName,'TextDB_CopyTable');
		return false;
	}

	if(file_exists($NewTableName) && is_file($NewTableName))
	{
		TextDB_SetErrorCode('W_2004',$NewTableName,'TextDB_CopyTable');
		return false;
	}

	/* 检测文件是否被锁定 */
	while(TextDB_isLockTable($TempTableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	/* 锁定数据表 */
	TextDB_LockTable($TempTableName,$TextDB_Link_DB_ID);

	/* 复制数据表 */
	if(!copy($TableName,$NewTableName))
	{
		/* 释放文件 */
		TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
		TextDB_SetErrorCode('E_2005',$TableName.' -> '.$NewTableName,'TextDB_CopyTable');
		return false;
	}

	$tblinfo = TextDB_SearchData(_TextDB_SysTBL_TableList_,"表名",$TempTableName,NULL,NULL,NULL,NULL,$TextDB_Link_DB_ID);
	TextDB_InsertData(_TextDB_SysTBL_TableList_,_TextDB_SysTBLF_TableList_,explode(",",$TempNewTableName.",".$tblinfo[0][2].",".$tblinfo[0][3].",".$tblinfo[0][4].",".$tblinfo[0][5]),$TextDB_Link_DB_ID);
	_ChangeTableSav('I',array($TempTableName,$TempNewTableName),$TextDB_Link_DB_ID);

	/* 释放文件 */
	TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

	return true;
}

/**
 * 清空数据表
 */
function TextDB_CleanTable($TableName,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CleanTable');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_CleanTable');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_CleanTable');
		return false;
	}

	/* 检测数据表名称是否符合要求 */
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_CleanTable');
		return false;
	}

	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	//检测表
	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_CleanTable');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_CleanTable');
			return false;
		}
	}

	/* 检测文件是否被锁定 */
	while(TextDB_isLockTable($TempTableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	/* 数据表锁定 */
	TextDB_LockTable($TempTableName,$TextDB_Link_DB_ID);

	/* 分析数据表结构 */
	$Fields    = explode(_TextDB_EXStr_,TextDB_ReadOneLine($TableName));

	/* 消除换行符号 */
	$Fields[sizeof($Fields)-1] = "";

	$NewFields = implode(_TextDB_EXStr_,$Fields);

	/* 清空数据 */
	TextDB_FileWrite($TableName,"w",$NewFields);

	/* 更新自增涨字段 */
	_TextDB_UpdateTableTrueNum($TempTableName,false,$TextDB_Link_DB_ID);

	_ChangeTableSav('R',$TempTableName,$TextDB_Link_DB_ID);

	/* 释放文件 */
	TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

	return true;
}

//获取 表 安全信息(外部调用)
function TextDB_GetHeaderInfo($TableName,$TextDB_Link_DB_ID)
{
	//检测文件是否被锁定
	while(TextDB_isLockTable($TableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TableName,$TextDB_Link_DB_ID);

	$result = _TextDB_GetHeaderInfo($TableName,$TextDB_Link_DB_ID=NULL);

	//释放文件
	TextDB_UnlockTable($TableName,$TextDB_Link_DB_ID);

	return $result;
}

//获取 表 字段（结构）(外部调用)
function _TextDB_GetHeaderInfo($TableName,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_GetHeaderInfo');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_GetHeaderInfo');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_GetHeaderInfo');
		return false;
	}

	/* 检测数据表名称是否符合要求 */
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_GetHeaderInfo');
		return false;
	}

	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	if(!file_exists($TableName) and !is_file($TableName))
	{
		if(file_exists($BakTableName) and is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_GetHeaderInfo');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_GetHeaderInfo');
			return false;
		}
	}

	/* 获取数据表信息 */
	$TableInfo = TextDB_ReadOneLine($TableName);

	/* 分析数据表安全信息 */
	$HeaderInfo = explode(_TextDB_EXStr_,$TableInfo);

	return $HeaderInfo[0];
}

//获取 表 字段（结构）(外部调用)
function TextDB_GetFields($TableName,$TextDB_Link_DB_ID=NULL)
{
	//检测文件是否被锁定
	while(TextDB_isLockTable($TableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TableName,$TextDB_Link_DB_ID);

	$result = _TextDB_GetFields($TableName,$TextDB_Link_DB_ID);

	//释放文件
	TextDB_UnlockTable($TableName,$TextDB_Link_DB_ID);

	return $result;
}

//获取 表 字段（结构）(内部调用)
function _TextDB_GetFields($TableName,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_GetFields');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_GetFields');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_GetFields');
		return false;
	}

	/* 检测数据表名称是否符合要求 */
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_GetFields');
		return false;
	}

	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_GetFields');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_GetFields');
			return false;
		}
	}

	/* 获取数据表信息 */
	$TableInfo = TextDB_ReadOneLine($TableName);

	/* 分析数据表结构 */
	$Fields = explode(_TextDB_EXStr_,$TableInfo);

	/* 整理表字段 */
	$NewFields = array();
	foreach($Fields as $FieldsID=>$FieldsName)
	{
		if($FieldsID==0 || $FieldsName==='')
		{
			continue;
		}
		$NewFields[$FieldsID-1] = TextDB_FilterStr($FieldsName,0);
	}


	/* 返回整理字段 */
	return $NewFields;
}

//更新表结构（外部调用）
function TextDB_UpdateFields($TableName,$Fields,$TextDB_Link_DB_ID=NULL)
{
	//检测文件是否被锁定
	while(TextDB_isLockTable($TableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TableName,$TextDB_Link_DB_ID);

	$result = _TextDB_UpdateFields($TableName,$Fields,$TextDB_Link_DB_ID);

	//释放文件
	TextDB_UnlockTable($TableName,$TextDB_Link_DB_ID);

	return $result;
}

/**
 * 更新表结构（内部调用）
 */
function _TextDB_UpdateFields($TableName,$Fields,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_UpdateFields');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_UpdateFields');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_UpdateFields');
		return false;
	}

	/* 检测数据表名称是否符合要求 */
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_UpdateFields');
		return false;
	}
	$Fields = TextDB_FilterStr($Fields);

	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	//检测表
	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_UpdateFields');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_UpdateFields');
			return false;
		}
	}

	//检测 是否为系统表
	if(TextDB_CheckStr($TempTableName,_TextDB_SysTBSN_,1))
	{
		TextDB_SetErrorCode('W_2006',$TempTableName,'TextDB_UpdateFields');
		return false;
	}

	//获取字段
	if(!is_array($Fields))
	{
		$Fields = explode(",",$Fields);
	}

	$Fields = _TextDB_GetHeaderInfo($TempTableName,$TextDB_Link_DB_ID)._TextDB_EXStr_.implode(_TextDB_EXStr_,$Fields)._TextDB_EXStr_."\n";

	//修改字段
	$tblinfo=TextDB_FileToArray($TableName);
	$tblinfo[0]=$Fields;
	$WriteContent=implode("",$tblinfo);
	TextDB_FileWrite($TableName,"w",$WriteContent);

	return true;
}

//获取表计数（外部调用）
function TextDB_GetTableTrueNum($TableName,$TextDB_Link_DB_ID=NULL)
{
	//检测文件是否被锁定
	while(TextDB_isLockTable($TableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TableName,$TextDB_Link_DB_ID);

	$result = _TextDB_GetTableTrueNum($TableName,$TextDB_Link_DB_ID);

	//释放文件
	TextDB_UnlockTable($TableName,$TextDB_Link_DB_ID);

	return $result;
}

//2007-11-15
//获取表计数（内部调用）
function _TextDB_GetTableTrueNum($TableName,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_GetTableTrueNum');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_GetTableTrueNum');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_GetTableTrueNum');
		return false;
	}

	//检测数据表名称是否符合要求
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_GetTableTrueNum');
		return false;
	}

	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	//检测表
	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_GetTableTrueNum');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_GetTableTrueNum');
			return false;
		}
	}

	//检测 是否为系统表
	if(TextDB_CheckStr($TempTableName,_TextDB_SysTBSN_,1))
	{
		TextDB_SetErrorCode('W_2006',$TempTableName,'TextDB_GetTableTrueNum');
		return false;
	}

	$TableInfo = TextDB_SearchData(_TextDB_SysTBL_TableList_,"表名",$TempTableName,NULL,NULL,NULL,NULL,$TextDB_Link_DB_ID);

	return $TableInfo[0]["计数"];
}

//更新表计数（外部调用）
function TextDB_UpdateTableTrueNum($TableName,$Mode=true,$TextDB_Link_DB_ID=NULL)
{
	//检测文件是否被锁定
	while(TextDB_isLockTable($TableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TableName,$TextDB_Link_DB_ID);

	$result = _TextDB_UpdateTableTrueNum($TableName,$Mode,$TextDB_Link_DB_ID);

	//释放文件
	TextDB_UnlockTable($TableName,$TextDB_Link_DB_ID);

	return $result;
}

//2007-11-15
//更新表计数(内部调用)
function _TextDB_UpdateTableTrueNum($TableName,$Mode=true,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_UpdateTableTrueNum');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_UpdateTableTrueNum');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_UpdateTableTrueNum');
		return false;
	}

	//检测数据表名称是否符合要求
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_UpdateTableTrueNum');
		return false;
	}

	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	//检测表
	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_UpdateTableTrueNum');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_UpdateTableTrueNum');
			return false;
		}
	}

	//检测 是否为系统表
	if(TextDB_CheckStr($TempTableName,_TextDB_SysTBSN_,1))
	{
		TextDB_SetErrorCode('W_2006',$TempTableName,'TextDB_UpdateTableTrueNum');
		return false;
	}

	$TableInfo = TextDB_SearchData(_TextDB_SysTBL_TableList_,"表名",$TempTableName,NULL,NULL,NULL,NULL,$TextDB_Link_DB_ID);

	if($Mode)
	{  //如果为更新模式
		TextDB_ChangeData(_TextDB_SysTBL_TableList_,"表名",$TempTableName,"计数",$TableInfo[0]["计数"]+1,$TextDB_Link_DB_ID);
	}
	else
	{  //如果为重置模式
		TextDB_ChangeData(_TextDB_SysTBL_TableList_,"表名",$TempTableName,"计数",0,$TextDB_Link_DB_ID);
	}

	return $TableInfo[0]["计数"]+1;
}


//获取 表 字段（结构）(外部调用)
function TextDB_CheakFields($TableName,$FieldsName,$TextDB_Link_DB_ID=NULL)
{
	//检测文件是否被锁定
	while(TextDB_isLockTable($TableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TableName,$TextDB_Link_DB_ID);

	$result = _TextDB_CheakFields($TableName,$FieldsName,$TextDB_Link_DB_ID);

	//释放文件
	TextDB_UnlockTable($TableName,$TextDB_Link_DB_ID);

	return $result;
}

//获取 表 字段（结构）(内部调用)
function _TextDB_CheakFields($TableName,$FieldsName,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_CheakFields');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_CheakFields');
		return false;
	}

	if(empty($FieldsName))
	{
		TextDB_SetErrorCode('W_3000',NULL,'TextDB_CheakFields');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_CheakFields');
		return false;
	}

	/* 检测数据表名称是否符合要求 */
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_CheakFields');
		return false;
	}

	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_CheakFields');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_CheakFields');
			return false;
		}
	}

	/* 获取数据表信息 */
	$TableInfo = TextDB_ReadOneLine($TableName);

	/* 分析数据表结构 */
	$Fields = explode(_TextDB_EXStr_,$TableInfo);

	/* 整理表字段 */
	$NewFields = array();
	foreach($Fields as $FieldsSN)
	{
		if($FieldsSN==$FieldsName)
		{
			return true;
		}
	}

	return false;
}

//插入字段
//-------------------------------------
//$TableName    要插入字段的表名
//$FieldsName   插入字段名称
//$FieldsSN     插入字段位置，可以用名称
//$FieldsPos    插入字段的前后
//$FieldsAll    是否更新所有的内容
//-------------------------------------
function TextDB_InsertFields($TableName,$FieldsName,$FieldsSN=0,$FieldsPos=true,$FieldsAll=true,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_InsertFields');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_InsertFields');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_InsertFields');
		return false;
	}

	/* 检测数据表名称是否符合要求 */
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_InsertFields');
		return false;
	}

	$FieldsName = TextDB_FilterStr($FieldsName);
	$FieldsSN   = TextDB_FilterStr($FieldsSN);

	/* 获取 数据表 名称和所属库 */
	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	//检测表
	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_InsertFields');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_InsertFields');
			return false;
		}
	}

	/* 检测文件是否被锁定 */
	while(TextDB_isLockTable($TempTableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	/* 锁定文件 */
	TextDB_LockTable($TempTableName,$TextDB_Link_DB_ID);

	$DBContent     = TextDB_FileToArray($TableName);
	$DBContentSize = sizeof($DBContent)-1;
	$DBFields      = explode(_TextDB_EXStr_,$DBContent[0]);
	$DBFieldSize   = sizeof($DBFields)-1;

	/* 获取是否按字段名称插入 */
	for($i=1;$i<=$DBFieldSize;$i++)
	{
		if($DBFields[$i]==$FieldsSN && !is_int($FieldsSN))
		{
			$FieldsSN = $i;
		}
	}

	/* 检测是否为已有字段 */
	for($i=1;$i<=$DBFieldSize;$i++)
	{
		if($DBFields[$i]==$FieldsName)
		{
			/* 释放文件 */
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_3004',$FieldsName,'TextDB_InsertFields');
			return false;
		}
	}

	/* 判断是否在结尾插入 */
	if($FieldsSN!=0)
	{
		if(is_int($FieldsSN))
		{
			/* 序号超出范围 */
			if($FieldsSN>$DBFieldSize)
			{
				/* 释放文件 */
				TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
				TextDB_SetErrorCode('W_3003',$FieldsSN.'>'.$DBFieldSize,'TextDB_InsertFields');
				return false;
			}

			/* 序号等于最大值在结尾插入 */
			if($FieldsSN==$DBFieldSize)
			{
				/* 释放文件 */
				TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

				/* 插入最后 */
				TextDB_InsertFields($TempTableName,$FieldsName,0,$FieldsPos,$FieldsAll,$TextDB_Link_DB_ID);
			}
			/* 判断是否数据也同步更新 */
			if($FieldsAll)
			{
				/* 判断插入在前还是后 */
				if(!$FieldsPos)
				{
					$TempFields[0]="";
					$j=0;

					/* 保留序号以后所有字段 */
					for($i=$FieldsSN;$i<=$DBFieldSize;$i++)
					{
						$j+=1;
						$TempFields[$j]=$DBFields[$i];
					}

					/* 替换序号字段 */
					$DBFields[$FieldsSN]=$FieldsName;

					/* 重新写入序号字段 */
					for($i=1;$i<sizeof($TempFields);$i++)
					{
						$DBFields[$FieldsSN+$i]=$TempFields[$i];
					}

					/* 汇合字段 */
					$DBContent[0]=implode(_TextDB_EXStr_,$DBFields);

					/* 同步处理数据 */
					for($i=1;$i<=$DBContentSize;$i++)
					{
						$DBData      = explode(_TextDB_EXStr_,$DBContent[$i]);
						$DBDataSize  = sizeof($DBData)-1;
						$TempData[0] = "";
						$k           = 0;

						for($j=$FieldsSN;$j<=$DBDataSize;$j++)
						{
							$k+=1;
							$TempData[$k]=$DBData[$j];
						}

						$DBData[$FieldsSN]="";

						for($j=1;$j<sizeof($TempData);$j++)
						{
							$DBData[$FieldsSN+$j]=$TempData[$j];
						}

						$DBContent[$i]=implode(_TextDB_EXStr_,$DBData);
					}
				}
				else
				{
					/* 序号加1模拟在后面加入 */
					$FieldsSN+=1;
					$TempFields[0]="";
					$j=0;
					for($i=$FieldsSN;$i<=$DBFieldSize;$i++)
					{
						$j+=1;
						$TempFields[$j]=$DBFields[$i];
					}

					$DBFields[$FieldsSN]=$FieldsName;

					for($i=1;$i<sizeof($TempFields);$i++)
					{
						$DBFields[$FieldsSN+$i]=$TempFields[$i];
					}

					$DBContent[0]=implode(_TextDB_EXStr_,$DBFields);

					for($i=1;$i<=$DBContentSize;$i++)
					{
						$DBData=explode(_TextDB_EXStr_,$DBContent[$i]);
						$DBDataSize=sizeof($DBData)-1;
						$TempData[0]="";
						$k=0;

						for($j=$FieldsSN;$j<=$DBDataSize;$j++)
						{
							$k+=1;
							$TempData[$k]=$DBData[$j];
						}

						$DBData[$FieldsSN]="";

						for($j=1;$j<sizeof($TempData);$j++)
						{
							$DBData[$FieldsSN+$j]=$TempData[$j];
						}

						$DBContent[$i]=implode(_TextDB_EXStr_,$DBData);
					}
				}
			}
			else
			{
				/* 不同步更新数据模式 */
				if(!$FieldsPos)
				{
					$TempFields[0]="";
					$j=0;

					for($i=$FieldsSN;$i<=$DBFieldSize;$i++)
					{
						$j+=1;
						$TempFields[$j]=$DBFields[$i];
					}

					$DBFields[$FieldsSN]=$FieldsName;

					for($i=1;$i<sizeof($TempFields);$i++)
					{
						$DBFields[$FieldsSN+$i]=$TempFields[$i];
					}

					$DBContent[0]=implode(_TextDB_EXStr_,$DBFields);
				}
				else
				{
					$FieldsSN+=1;
					$TempFields[0]="";
					$j=0;

					for($i=$FieldsSN;$i<=$DBFieldSize;$i++)
					{
						$j+=1;
						$TempFields[$j]=$DBFields[$i];
					}

					$DBFields[$FieldsSN]=$FieldsName;

					for($i=1;$i<sizeof($TempFields);$i++)
					{
						$DBFields[$FieldsSN+$i]=$TempFields[$i];
					}

					$DBContent[0]=implode(_TextDB_EXStr_,$DBFields);
				}
			}
		}
	}
	else
	{
		/* 在结尾插入字段 */
		if($FieldsAll)
		{
			if(!$FieldsPos)
			{
				/* 在结尾字段前插入 */
				/* 增加字段，内容为最后的字段 */
				$DBFields[$DBFieldSize+1] = $DBFields[$DBFieldSize];

				/* 修改最后字段，为最后第2个字段 */
				$DBFields[$DBFieldSize] = $DBFields[$DBFieldSize-1];

				/* 修改最后第2个字段为，插入字段 */
				$DBFields[$DBFieldSize-1] = $FieldsName;

				/* 汇合字段 */
				$DBContent[0] = implode(_TextDB_EXStr_,$DBFields);

				/* 同步更新数据 */
				for($i=1;$i<=$DBContentSize;$i++)
				{
					$DBData                = explode(_TextDB_EXStr_,$DBContent[$i]);
					$DBDataSize            = sizeof($DBData)-1;
					$DBData[$DBDataSize+1] = $DBData[$DBDataSize];
					$DBData[$DBDataSize]   = $DBData[$DBDataSize-1];
					$DBData[$DBDataSize-1] = "";
					$DBContent[$i]         = implode(_TextDB_EXStr_,$DBData);
				}
			}
			else
			{
				/* 在结尾后插入字段 */
				/* 增加字段，内容为最后的字段 */
				$DBFields[$DBFieldSize+1] = $DBFields[$DBFieldSize];

				/* 修改最后字段，为插入字段 */
				$DBFields[$DBFieldSize] = $FieldsName;

				/* 汇合字段 */
				$DBContent[0] = implode(_TextDB_EXStr_,$DBFields);

				/* 同步更新数据 */
				for($i=1;$i<=$DBContentSize;$i++)
				{
					$DBData                = explode(_TextDB_EXStr_,$DBContent[$i]);
					$DBDataSize            = sizeof($DBData)-1;
					$DBData[$DBDataSize+1] = $DBData[$DBDataSize];
					$DBData[$DBDataSize]   = "";
					$DBContent[$i]         = implode(_TextDB_EXStr_,$DBData);
				}
			}
		}
		else
		{
			/* 不同步更新数据 */
			if(!$FieldsPos)
			{
				$DBFields[$DBFieldSize+1] = $DBFields[$DBFieldSize];
				$DBFields[$DBFieldSize]   = $DBFields[$DBFieldSize-1];
				$DBFields[$DBFieldSize-1] = $FieldsName;
				$DBContent[0]             = implode(_TextDB_EXStr_,$DBFields);
			}
			else
			{
				$DBFields[$DBFieldSize+1] = $DBFields[$DBFieldSize];
				$DBFields[$DBFieldSize]   = $FieldsName;
				$DBContent[0]             = implode(_TextDB_EXStr_,$DBFields);
			}
		}
	}

	/* 汇合所有数据 */
	$WriteContent = implode("",$DBContent);

	/* 写入数据 */
	TextDB_FileWrite($TableName,"w",$WriteContent);

	/* 释放文件 */
	TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

	return true;
}

//删除字段
//------------------------------------------
//$TableName    要删除字段的表名
//$FieldsSN     删除字段序号(名称)，可以是数组
//$FieldsAll    是否更新所有的内容
//------------------------------------------
function TextDB_DeleteFields($TableName,$FieldsSN,$FieldsAll=true,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_DeleteFields');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_DeleteFields');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_DeleteFields');
		return false;
	}

	//检测数据表名称是否符合要求
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_DeleteFields');
		return false;
	}

	$FieldsSN = TextDB_FilterStr($FieldsSN);

	//获取 数据表 名称和所属库
	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	//检测表
	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_DeleteFields');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_DeleteFields');
			return false;
		}
	}

	//检测文件是否被锁定
	while(TextDB_isLockTable($TempTableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TempTableName,$TextDB_Link_DB_ID);

	$DBContent     = TextDB_FileToArray($TableName);
	$DBContentSize = sizeof($DBContent)-1;
	$DBFields      = explode(_TextDB_EXStr_,$DBContent[0]);
	$DBFieldSize   = sizeof($DBFields)-1;

	//获取是否按字段名称插入
	if(is_array($FieldsSN))
	{
		for($i=0;$i<sizeof($FieldsSN);$i++)
		{
			for($j=1;$j<=$DBFieldSize;$j++)
			{
				if($DBFields[$j]==TextDB_FilterStr($FieldsSN[$i]))
				{
					$FieldsSN[$i]=$j;
				}
			}
			if(!is_int($FieldsSN[$i]))
			{
				//释放文件
				TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
				TextDB_SetErrorCode('W_3003',$FieldsSN[$i],'TextDB_DeleteFields');
				return false;
			}
		}
	}
	else
	{
		for($i=1;$i<=$DBFieldSize;$i++)
		{
			if($DBFields[$i]==TextDB_FilterStr($FieldsSN))
			{
				$FieldsSN=$i;
			}
		}
		if(!is_int($FieldsSN))
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_3003',$FieldsSN,'TextDB_DeleteFields');
			return false;
		}
	}

	//删除字段
	if(is_array($FieldsSN))
	{
		//删除多个字段
		if($FieldsAll)
		{
			//连同数据一起删除下
			//删除字段内容
			for($i=0;$i<sizeof($FieldsSN);$i++)
			{
				$DBFields[$FieldsSN[$i]]="";
			}

			//合并字段内容
			$j=0;
			for($i=0;$i<=$DBFieldSize;$i++)
			{
				//检测是否以被删除
				if(!empty($DBFields[$i]) or $i==0 or $i==$DBFieldSize)
				{
					$TempFields[$j]=$DBFields[$i];
					$j+=1;
				}
			}
			$DBFields=$TempFields;

			//汇合字段
			$DBContent[0]=implode(_TextDB_EXStr_,$DBFields);

			//卸载已汇合字段信息
			unset($DBFields,$TempFields);

			//汇合数据
			for($i=1;$i<=$DBContentSize;$i++)
			{
				if(!empty($DBContent[$i])){
					$DBData     = explode(_TextDB_EXStr_,$DBContent[$i]);
					$DBDataSize = sizeof($DBData)-1;

					//删除数据
					for($j=0;$j<sizeof($FieldsSN);$j++)
					{
						$DBData[$FieldsSN[$j]]="";
					}

					//合并数据内容
					$k = 0;
					for($j=0;$j<=$DBDataSize;$j++)
					{
						if(!empty($DBData[$j]) || $j==0 || $j==$DBDataSize)
						{
							$TempData[$k]=$DBData[$j];
							$k+=1;
						}
					}
					$DBData=$TempData;

					//汇合数据
					$DBContent[$i]=implode(_TextDB_EXStr_,$DBData);

					//*****卸载已汇合数据，防止出现异常****
					unset($DBData,$TempData);
				}
			}
		}
		else
		{
			//不删除数据
			//删除字段
			for($i=0;$i<sizeof($FieldsSN);$i++)
			{
				$DBFields[$FieldsSN[$i]]="";
			}

			//合并字段
			$j=0;
			for($i=0;$i<=$DBFieldSize;$i++)
			{
				//检测是否已删除
				if(!empty($DBFields[$i]) or $i==0 or $i==$DBFieldSize)
				{
					$TempFields[$j]=$DBFields[$i];
					$j+=1;
				}
			}
			$DBFields=$TempFields;

			//汇合字段
			$DBContent[0]=implode(_TextDB_EXStr_,$DBFields);

			//删除汇合字段信息
			unset($DBFields,$TempFields);
		}
	}
	else
	{
		//删除单个字段
		if($FieldsAll)
		{
			//连同数据删除模式
			//删除字段
			$DBFields[$FieldsSN]="";

			//合并字段
			$j=0;
			for($i=0;$i<=$DBFieldSize;$i++)
			{
				//检测是否已删除
				if(!empty($DBFields[$i]) || $i==0 || $i==$DBFieldSize)
				{
					$TempFields[$j]=$DBFields[$i];
					$j+=1;
				}
			}
			$DBFields=$TempFields;

			//汇合字段
			$DBContent[0]=implode(_TextDB_EXStr_,$DBFields);

			//删除汇合字段信息
			unset($DBFields,$TempFields);

			//删除数据
			for($i=1;$i<=$DBContentSize;$i++)
			{
				$DBData=explode(_TextDB_EXStr_,$DBContent[$i]);
				$DBDataSize=sizeof($DBData)-1;
				//删除数据
				$DBData[$FieldsSN]="";

				//合并数据
				$k=0;
				for($j=0;$j<=$DBDataSize;$j++)
				{
					//检测是否已删除
					if(!empty($DBData[$j]) or $j==0 or $j==$DBDataSize)
					{
						$TempData[$k]=$DBData[$j];
						$k+=1;
					}
				}
				$DBData=$TempData;

				//汇合数据
				$DBContent[$i]=implode(_TextDB_EXStr_,$DBData);

				//*****卸载已汇合数据，防止出现异常****
				unset($DBData,$TempData);
			}
		}
		else
		{
			//不删除数据
			//删除字段
			$DBFields[$FieldsSN]="";

			//合并字段
			$j=0;
			for($i=0;$i<=sizeof($DBFields);$i++)
			{
				//检测是否已删除
				if(!empty($DBFields[$i]) or $i==0 or $i==$DBFieldSize)
				{
					$TempFields[$j]=$DBFields[$i];
					$j+=1;
				}
			}
			$DBFields=$TempFields;

			//汇合字段
			$DBContent[0]=implode(_TextDB_EXStr_,$DBFields);

			//删除汇合字段信息
			unset($DBFields,$TempFields);
		}
	}

	//汇合所有数据
	$WriteContent = implode("",$DBContent);
	//写入数据
	TextDB_FileWrite($TableName,"w",$WriteContent);

	//释放文件
	TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

	return true;
}

//修改字段
//------------------------------------------
//$TableName    要修改字段的表名
//$FieldsSN     被替换字段名称
//$FieldsName   替换的字段名称
//------------------------------------------
function TextDB_ChangeFields($TableName,$FieldsSN,$FieldsName,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_ChangeFields');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_ChangeFields');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_ChangeFields');
		return false;
	}

	//获取 数据表 名称和所属库
	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	//检测表
	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_ChangeFields');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_ChangeFields');
			return false;
		}
	}

	//检测文件是否被锁定
	while(TextDB_isLockTable($TempTableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TempTableName,$TextDB_Link_DB_ID);

	$DBContent   = TextDB_FileToArray($TableName);
	$DBFields    = explode(_TextDB_EXStr_,$DBContent[0]);
	$DBFieldSize = sizeof($DBFields)-1;

	//检测是否提交完善的修改数据
	if(empty($FieldsSN))
	{
		//释放文件
		TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
		TextDB_SetErrorCode('W_3000',NULL,'TextDB_ChangeFields');
		return false;
	}

	if(empty($FieldsName))
	{
		//释放文件
		TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
		TextDB_SetErrorCode('W_3001',NULL,'TextDB_ChangeFields');
		return false;
	}

	//获取是否按字段名称修改
	if(is_array($FieldsSN))
	{
		for($i=0;$i<sizeof($FieldsSN);$i++)
		{
			for($j=1;$j<=$DBFieldSize;$j++)
			{
				if($DBFields[$j]==TextDB_FilterStr($FieldsSN[$i]))
				{
					$FieldsSN[$i]=$j;
				}
			}
			//如果不存在字段，返回
			if(!is_int($FieldsSN[$i]))
			{
				//释放文件
				TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
				TextDB_SetErrorCode('W_3003',$FieldsSN[$i],'TextDB_ChangeFields');
				return false;
			}
		}
	}
	else
	{
		for($i=1;$i<=$DBFieldSize;$i++)
		{
			if($DBFields[$i]==TextDB_FilterStr($FieldsSN))
			{
				$FieldsSN=$i;
			}
		}
		//如果不存在字段，返回
		if(!is_int($FieldsSN))
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_3003',$FieldsSN,'TextDB_ChangeFields');
			return false;
		}
	}

	//修改字段
	if(is_array($FieldsSN))
	{
		for($i=0;$i<=sizeof($FieldsSN);$i++)
		{
			$DBFields[$FieldsSN[$i]]=TextDB_FilterStr($FieldsName[$i]);
		}
	}
	else
	{
		$DBFields[$FieldsSN]=TextDB_FilterStr($FieldsName);
	}

	//汇合字段
	$DBContent[0]=implode(_TextDB_EXStr_,$DBFields);

	//删除汇合字段信息
	unset($DBFields);

	//汇合所有数据
	$WriteContent=implode("",$DBContent);

	//写入数据
	TextDB_FileWrite($TableName,"w",$WriteContent);

	//释放文件
	TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

	return true;
}

/**
 * 数据操作，总记录表变更
 * Type 说明
 * I 插入记录
 * D 删除记录
 */
function _ChangeDataSav($Type,$TableName,$TextDB_Link_DB_ID=NULL)
{
	$TextDB_SYSDB_Link_ID = TextDB_ConnectDB(_TextDB_SysDB_System_);
	$DBName   = TextDB_GetLinkDBName($TextDB_Link_DB_ID);
	$IS_SYSDB = false;
	if(TextDB_CheckStr($DBName,_TextDB_SysDBSN_,1))
	{
		$IS_SYSDB = true;
	}
	else
	{
		$DBInfo = TextDB_SearchData(_TextDB_SysTBL_DataBaseList_,'库名',$DBName,"asc",NULL,NULL,NULL,$TextDB_SYSDB_Link_ID);
		$DBInfo = $DBInfo[0];
	}

	$TableInfo = TextDB_SearchData(_TextDB_SysTBL_TableList_,'表名',$TableName,NULL,NULL,NULL,NULL,$TextDB_Link_DB_ID);
	$TableInfo = $TableInfo[0];
	$Info  = array($TableInfo['表名'],$TableInfo['所属库']);

	if($Type==='I')
	{
		TextDB_ChangeData(_TextDB_SysTBL_TableList_,'表名',$TableName,'行数',$TableInfo['行数']+1,$TextDB_Link_DB_ID);

		$Info2 = array($TableInfo['表名'],$TableInfo['建立者'],$TableInfo['所属库'],$TableInfo['开放'],$TableInfo['计数'],$TableInfo['行数']+1,$TableInfo['建立日期'],$TableInfo['回收日期'],$TableInfo['还原日期'],1);
		TextDB_ChangeData(_TextDB_SysTBL_DBTableList_,'表名,所属库',$Info,'表名,建立者,所属库,开放,计数,行数,建立日期,回收日期,还原日期,当前状态',$Info2,$TextDB_Link_DB_ID);
		TextDB_ChangeData(_TextDB_SysTBL_AllTableList_,'表名,所属库',$Info,'表名,建立者,所属库,开放,计数,行数,建立日期,回收日期,还原日期,当前状态',$Info2,$TextDB_SYSDB_Link_ID);

		if(!$IS_SYSDB)
		{
			/* 变更库记录 */
			TextDB_ChangeData(_TextDB_SysTBL_DataBaseList_,'库名',$DBName,'行数',$DBInfo['行数']+1,$TextDB_SYSDB_Link_ID);
		}
	}
	elseif($Type==='D')
	{
		TextDB_ChangeData(_TextDB_SysTBL_TableList_,'表名',$TableName,'行数',$TableInfo['行数']-1,$TextDB_Link_DB_ID);

		$Info2 = array($TableInfo['表名'],$TableInfo['建立者'],$TableInfo['所属库'],$TableInfo['开放'],$TableInfo['计数'],$TableInfo['行数']-1,$TableInfo['建立日期'],$TableInfo['回收日期'],$TableInfo['还原日期'],1);
		TextDB_ChangeData(_TextDB_SysTBL_DBTableList_,'表明,所属库',$Info,'表名,建立者,所属库,开放,计数,行数,建立日期,回收日期,还原日期,当前状态',$Info2,$TextDB_Link_DB_ID);
		TextDB_ChangeData(_TextDB_SysTBL_AllTableList_,'表明,所属库',$Info,'表名,建立者,所属库,开放,计数,行数,建立日期,回收日期,还原日期,当前状态',$Info2,$TextDB_SYSDB_Link_ID);

		if(!$IS_SYSDB)
		{
			/* 变更库记录 */
			TextDB_ChangeData(_TextDB_SysTBL_DataBaseList_,'库名',$DBName,'行数',$DBInfo['行数']-1,$TextDB_SYSDB_Link_ID);
		}
	}

	TextDB_ConnectClose($TextDB_SYSDB_Link_ID);
	return true;
}

//插入数据
//-------------------------------
//$TableName   数据表名称
//$Fields      欲增添字段名
//$Values      欲增添数据
//-------------------------------
function TextDB_InsertData($TableName,$Fields,$Values,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_InsertData');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_InsertData');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_InsertData');
		return false;
	}

	//检测数据表名称是否符合要求
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_InsertData');
		return false;
	}

	//获取 数据表 名称和所属库
	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	//检测表
	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_InsertData');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_InsertData');
			return false;
		}
	}

	//检测文件是否被锁定
	while(TextDB_isLockTable($TempTableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TempTableName,$TextDB_Link_DB_ID);

	$TableFields     = explode(_TextDB_EXStr_,TextDB_ReadOneLine($TableName));
	$TableFieldsSize = sizeof($TableFields);

	//初始化数据列表
	//检测 是否为系统表
	if(TextDB_CheckStr($TempTableName,_TextDB_SysTBSN_,1))
	{
		$data[0] = "\n";
	}
	else
	{
		$data[0] = "\n"._TextDB_UpdateTableTrueNum($TempTableName,true,$TextDB_Link_DB_ID);
	}

	for($i = 1; $i < $TableFieldsSize; $i++)
	{
		$data[$i] = "";
	}

	//2007-10-25
	//如果字段为 * 默认获取全部字段
	if($Fields == "*")
	{
		$Fields = _TextDB_GetFields($TempTableName,$TextDB_Link_DB_ID);
		$Fields = implode(",",$Fields);
	}

	if(strstr($Fields,","))
	{
		$Fields = explode(",",$Fields);
	}

	//模拟字段数组
	if(is_array($Fields))
	{
		//多个字段添加
		//检测字段和数据数据类型是否对应
		if(!is_array($Values))
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_4000',NULL,'TextDB_InsertData');
			return false;
		}

		//获取字段个数，欲填字段
		$FieldsSize = sizeof($Fields)-1;

		//获取数据个数，欲填数据
		$ValuesSize = sizeof($Values)-1;

		//检测字段，数据是否对应
		if($FieldsSize != $ValuesSize)
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_4000',$FieldsSize.' <> '.$ValuesSize,'TextDB_InsertData');
			return false;
		}

		//检测字段是否对应
		if($FieldsSize < $TableFieldsSize || $Fields == 0)
		{
			//获取字段ID
			for($i = 0; $i <= $FieldsSize; $i++)
			{
				for($j = 1; $j < $TableFieldsSize; $j++)
				{
					//如果字段存在，赋予ID
					if(TextDB_FilterStr($Fields[$i]) == $TableFields[$j])
					{
						$Fields[$i] = $j;
					}
				}

				//如果字段不为ID，就表明字段不存在，返回错误
				if(!is_int($Fields[$i]))
				{
					//释放文件
					TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
					TextDB_SetErrorCode('W_3003',$Fields[$i],'TextDB_InsertData');
					return false;
				}
			}
		}
		else
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_4000',NULL,'TextDB_InsertData');
			return false;
		}

		//串联数据
		for($i = 0; $i <= $FieldsSize; $i++)
		{
			$data[$Fields[$i]] = TextDB_FilterStr($Values[$i]);
		}
	}
	else
	{
		//非多个字段添加
		//检测字段和数据数据类型是否对应
		if(is_array($Values))
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_4000',NULL,'TextDB_InsertData');
			return false;
		}

		for($i = 1;$i < $TableFieldsSize; $i++)
		{
			//如果字段存在，赋予ID
			if(TextDB_FilterStr($Fields) == $TableFields[$i])
			{
				$Fields = $i;
			}
		}

		//如果字段不为ID，就表明字段不存在，返回错误
		if(!is_int($Fields))
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_3003',$Fields,'TextDB_InsertData');
			return false;
		}

		//串联数据
		$data[$Fields] = TextDB_FilterStr($Values);
	}

	//组合数据
	$WriteContent = implode(_TextDB_EXStr_,$data);
	//写入数据
	TextDB_FileWrite($TableName,"a",$WriteContent);

	if(!TextDB_CheckStr($TempTableName,_TextDB_SysTBSN_,1))
	{
		_ChangeDataSav('I',$TempTableName,$TextDB_Link_DB_ID);
	}

	//释放文件
	TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

	return true;
}

//删除数据
//-------------------------------
//$TableName      数据表名
//$Fields         字段名
//$Values         数值名
//-------------------------------
function TextDB_DeleteData($TableName,$Fields,$Values,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_DeleteData');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_DeleteData');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_DeleteData');
		return false;
	}

	//检测数据表名称是否符合要求
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_DeleteData');
		return false;
	}

	//获取 数据表 名称和所属库
	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	//检测表
	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_DeleteData');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_DeleteData');
			return false;
		}
	}

	//检测文件是否被锁定
	while(TextDB_isLockTable($TempTableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TempTableName,$TextDB_Link_DB_ID);

	$TableContent     = TextDB_FileToArray($TableName);
	$TableContentSize = sizeof($TableContent)-1;;
	$TableFields      = explode(_TextDB_EXStr_,TextDB_ReadOneLine($TableName));
	$TableFieldsSize  = sizeof($TableFields);

	//2007-10-30
	//如果字段为 * 默认获取全部字段
	if($Fields == "*")
	{
		$Fields = _TextDB_GetFields($TempTableName,$TextDB_Link_DB_ID);
		$Fields = implode(",",$Fields);
	}

	if(strstr($Fields,","))
	{
		$Fields = explode(",",$Fields);
	}

	//模拟字段数组
	if(is_array($Fields))
	{
		//多个字段添加
		//检测字段和数据数据类型是否对应
		if(!is_array($Values))
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_4000',NULL,'TextDB_DeleteData');
			return false;
		}

		//获取字段个数，欲填字段
		$FieldsSize = sizeof($Fields)-1;

		//获取数据个数，欲填数据
		$ValuesSize = sizeof($Values)-1;

		//检测字段，数据是否对应
		if($FieldsSize != $ValuesSize)
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_4000',$FieldsSize.' <> '.$ValuesSize,'TextDB_DeleteData');
			return false;
		}

		//检测字段是否对应
		if($FieldsSize < $TableFieldsSize or $Fields == 0)
		{
			//获取字段ID
			for($i = 0; $i <= $FieldsSize; $i++)
			{
				if($Fields[$i] != "TEXTDB_HID")
				{
					for($j = 1; $j < $TableFieldsSize; $j++)
					{
						//如果字段存在，赋予ID
						if(TextDB_FilterStr($Fields[$i]) == $TableFields[$j])
						{
							$Fields[$i] = $j;
						}
					}
				}
				else
				{
					$Fields[$i] = 0;
				}

				//如果字段不为ID，就表明字段不存在，返回错误
				if(!is_int($Fields[$i]))
				{
					//释放文件
					TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
					TextDB_SetErrorCode('W_3003',$Fields[$i],'TextDB_DeleteData');
					return false;
				}
			}
		}
		else
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_4000',NULL,'TextDB_DeleteData');
			return false;
		}

		//查找数据并清除
		$IN = 0;
		for($i = 1; $i <= $TableContentSize; $i++)
		{
			$k = 0;
			$TableData = explode(_TextDB_EXStr_,$TableContent[$i]);

			//检测是否有符合多条件数据
			for($j = 0; $j <= $FieldsSize; $j++)
			{
				if(TextDB_CheckWhere(TextDB_FilterStr($Values[$j]),$TableData[$Fields[$j]]))
				{
					$k+=1;
				}
			}

			//测试数据-1，同等sizeof($Fields)-1
			if($k-1 == $FieldsSize)
			{
				$TableContent[$i] = "D";
				$IN = 1;
			}
		}
	}
	else
	{
		//非多个字段添加
		//检测字段和数据数据类型是否对应
		if(is_array($Values))
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_4000',NULL,'TextDB_DeleteData');
			return false;
		}

		if($Fields != "TEXTDB_HID")
		{
			for($i = 1; $i < $TableFieldsSize; $i++)
			{
				//如果字段存在，赋予ID
				if(TextDB_FilterStr($Fields) == $TableFields[$i])
				{
					$Fields = $i;
				}
			}
		}
		else
		{
			$Fields = 0;
		}

		//如果字段不为ID，就表明字段不存在，返回错误
		if(!is_int($Fields))
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_3003',$Fields,'TextDB_DeleteData');
			return false;
		}

		//查找数据并清除
		$IN = 0;
		for($i = 1; $i <= $TableContentSize; $i++)
		{
			$TableData = explode(_TextDB_EXStr_,$TableContent[$i]);
			if(TextDB_CheckWhere(TextDB_FilterStr($Values),$TableData[$Fields]))
			{
				$TableContent[$i] = "D";
				$IN = 1;
			}
		}
	}
	//检测是否有可删除数据
	if(!$IN)
	{
		//释放文件
		TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
		TextDB_SetErrorCode('N_4000',NULL,'TextDB_DeleteData');
		return false;
	}

	//合并新数据
	$NewTableContent[0] = $TableContent[0];
	$j = 0;
	for($i = 1; $i <= $TableContentSize; $i++)
	{
		if($TableContent[$i] != "D"){
			$j+=1;
			$NewTableContent[$j] = $TableContent[$i];
		}
	}

	//分析最后的数据
	$LastData = explode(_TextDB_EXStr_,$NewTableContent[sizeof($NewTableContent)-1]);

	//消除换行符号
	$LastData[sizeof($LastData)-1] = "";

	//合并最后的数据
	$NewTableContent[sizeof($NewTableContent)-1] = implode(_TextDB_EXStr_,$LastData);

	$WriteContent = implode("",$NewTableContent);

	//写入数据
	TextDB_FileWrite($TableName,"w",$WriteContent);

	if(!TextDB_CheckStr($TempTableName,_TextDB_SysTBSN_,1))
	{
		_ChangeDataSav('D',$TempTableName,$TextDB_Link_DB_ID);
	}

	//释放文件
	TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

	return true;
}

//更新数据(修改数据别名)
function TextDB_UpdateData($TableName,$SearchFields=NULL,$SearchValues=NULL,$Fields=NULL,$Values=NULL,$TextDB_Link_DB_ID=NULL)
{
	return TextDB_ChangeData($TableName,$SearchFields,$SearchValues,$Fields,$Values,$TextDB_Link_DB_ID);
}

//修改数据
function TextDB_ChangeData($TableName,$SearchFields=NULL,$SearchValues=NULL,$Fields=NULL,$Values=NULL,$TextDB_Link_DB_ID=NULL)
{
	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_ChangeData');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_ChangeData');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_ChangeData');
		return false;
	}

	//检测数据表名称是否符合要求
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_ChangeData');
		return false;
	}

	//获取 数据表 名称和所属库
	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	//检测表
	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_ChangeData');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_ChangeData');
			return false;
		}
	}

	//检测文件是否被锁定
	while(TextDB_isLockTable($TempTableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TempTableName,$TextDB_Link_DB_ID);

	$TableContent     = TextDB_FileToArray($TableName);
	$TableContentSize = sizeof($TableContent)-1;
	$TableFields      = explode(_TextDB_EXStr_,TextDB_ReadOneLine($TableName));
	$TableFieldsSize  = sizeof($TableFields);

	/**
	 * 修正：2009-12-23
	 * 搜索字段整理
	 */
	if($SearchFields=="*")
	{
		$SearchFields = _TextDB_GetFields($TempTableName,$TextDB_Link_DB_ID);
		$SearchFields = implode(",",$SearchFields);
	}

	if(!is_array($SearchFields) && strstr($SearchFields,","))
	{
		$SearchFields = explode(",",$SearchFields);
	}
	
	/**
	 * 修正：2007-10-30
	 * 修改字段整理
	 */
	if($Fields=="*")
	{
		$Fields = _TextDB_GetFields($TempTableName,$TextDB_Link_DB_ID);
		$Fields = implode(",",$Fields);
	}

	if(!is_array($Fields) && strstr($Fields,","))
	{
		$Fields = explode(",",$Fields);
	}

	//模拟字段数组
	if(is_array($SearchFields))
	{
		//多个字段添加
		//检测字段和数据数据类型是否对应
		if(!is_array($SearchValues))
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_4000',NULL,'TextDB_ChangeData');
			return false;
		}

		//获取字段个数，欲填字段
		$SearchFieldsSize = sizeof($SearchFields)-1;

		//获取数据个数，欲填数据
		$SearchValuesSize = sizeof($SearchValues)-1;

		//检测字段，数据是否对应
		if($SearchFieldsSize!=$SearchValuesSize)
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_4000',$SearchFieldsSize.' <> '.$SearchValuesSize,'TextDB_ChangeData');
			return false;
		}

		//检测字段是否对应
		if($SearchFieldsSize<$TableFieldsSize || $SearchFields==0)
		{
			//获取字段ID
			for($i=0;$i<=$SearchFieldsSize;$i++)
			{
				if($SearchFields[$i]!="TEXTDB_HID")
				{
					for($j=1;$j<$TableFieldsSize;$j++)
					{
						//如果字段存在，赋予ID
						if(TextDB_FilterStr($SearchFields[$i])==$TableFields[$j])
						{
							$SearchFields[$i]=$j;
						}
					}
				}
				else
				{
					$SearchFields[$i]=0;
				}

				//如果字段不为ID，就表明字段不存在，返回错误
				if(!is_int($SearchFields[$i]))
				{
					//释放文件
					TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
					TextDB_SetErrorCode('W_3003',$SearchFields[$i],'TextDB_ChangeData');
					return false;
				}
			}
		}
		else
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_4000',NULL,'TextDB_ChangeData');
			return false;
		}

		//查找数据并返回
		$IN=0;
		for($i=1;$i<=$TableContentSize;$i++)
		{
			$k=0;
			$TableData=explode(_TextDB_EXStr_,$TableContent[$i]);

			//检测是否有符合多条件数据
			for($j=0;$j<=$SearchFieldsSize;$j++)
			{
				if(TextDB_CheckWhere(TextDB_FilterStr($SearchValues[$j]),$TableData[$SearchFields[$j]]))
				{
					$k+=1;
				}
			}
			//测试数据-1，同等sizeof($Fields)-1
			if($k-1==$SearchFieldsSize)
			{

				//模拟字段数组
				if(is_array($Fields))
				{
					//多个字段添加
					//检测字段和数据数据类型是否对应
					if(!is_array($Values))
					{
						//释放文件
						TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
						TextDB_SetErrorCode('W_4000',NULL,'TextDB_ChangeData');
						return false;
					}

					//获取字段个数，欲填字段
					$FieldsSize = sizeof($Fields)-1;

					//获取数据个数，欲填数据
					$ValuesSize = sizeof($Values)-1;

					//检测字段，数据是否对应
					if($FieldsSize!=$ValuesSize)
					{
						//释放文件
						TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
						TextDB_SetErrorCode('W_4000',$FieldsSize.' <> '.$ValuesSize,'TextDB_ChangeData');
						return false;
					}

					//检测字段是否对应
					if($FieldsSize<$TableFieldsSize or $Fields==0)
					{
						//获取字段ID
						for($j=0;$j<=$FieldsSize;$j++)
						{
							if($Fields[$j]!="TEXTDB_HID")
							{
								for($k=1;$k<$TableFieldsSize;$k++)
								{
									//如果字段存在，赋予ID
									if(TextDB_FilterStr($Fields[$j])==$TableFields[$k])
									{
										$Fields[$j]=$k;
									}
								}
							}
							else
							{
								$Fields[$j]=0;
							}

							//如果字段不为ID，就表明字段不存在，返回错误
							if(!is_int($Fields[$j]))
							{
								//释放文件
								TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
								TextDB_SetErrorCode('W_3003',$Fields[$j],'TextDB_ChangeData');
								return false;
							}
						}
					}
					else
					{
						//释放文件
						TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
						TextDB_SetErrorCode('W_4000',NULL,'TextDB_ChangeData');
						return false;
					}

					//分析数据
					$TempData = explode(_TextDB_EXStr_,$TableContent[$i]);

					//修改数据
					for($l=0;$l<=$FieldsSize;$l++)
					{
						$TempData[$Fields[$l]] = TextDB_FilterStr($Values[$l]);
					}
					$TableContent[$i] = implode(_TextDB_EXStr_,$TempData);

					unset($TempData);
					$IN=1;

				}
				else
				{
					//非多个字段添加
					//检测字段和数据数据类型是否对应
					if(is_array($Values))
					{
						//释放文件
						TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
						TextDB_SetErrorCode('W_4000',NULL,'TextDB_ChangeData');
						return false;
					}

					if($Fields!="TEXTDB_HID")
					{
						for($j=1;$j<$TableFieldsSize;$j++)
						{
							//如果字段存在，赋予ID
							if(TextDB_FilterStr($Fields)==$TableFields[$j])
							{
								$Fields=$j;
							}
						}
					}
					else
					{
						$Fields=0;
					}

					//如果字段不为ID，就表明字段不存在，返回错误
					if(!is_int($Fields))
					{
						//释放文件
						TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
						TextDB_SetErrorCode('W_3003',$Fields,'TextDB_ChangeData');
						return false;
					}

					//分析数据
					$TempData = explode(_TextDB_EXStr_,$TableContent[$i]);

					//修改数据
					$TempData[$Fields] = TextDB_FilterStr($Values);
					$TableContent[$i]  = implode(_TextDB_EXStr_,$TempData);

					unset($TempData);
					$IN=1;
				}
			}
		}
	}
	else
	{
		//非多个字段添加
		//检测字段和数据数据类型是否对应
		if(is_array($SearchValues))
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_4000',NULL,'TextDB_ChangeData');
			return false;
		}

		if($SearchFields!="TEXTDB_HID")
		{
			for($i=1;$i<$TableFieldsSize;$i++)
			{
				//如果字段存在，赋予ID
				if(TextDB_FilterStr($SearchFields)==$TableFields[$i])
				{
					$SearchFields = $i;
				}
			}
		}
		else
		{
			$SearchFields = 0;
		}

		//如果字段不为ID，就表明字段不存在，返回错误
		if(!is_int($SearchFields))
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
			TextDB_SetErrorCode('W_3003',$SearchFields,'TextDB_ChangeData');
			return false;
		}

		//查找数据并返回
		$IN=0;
		for($i=1;$i<=$TableContentSize;$i++)
		{
			$TableData = explode(_TextDB_EXStr_,$TableContent[$i]);
			if(TextDB_CheckWhere(TextDB_FilterStr($SearchValues),$TableData[$SearchFields]))
			{
				//模拟字段数组
				if(is_array($Fields))
				{
					//多个字段添加
					//检测字段和数据数据类型是否对应
					if(!is_array($Values))
					{
						//释放文件
						TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
						TextDB_SetErrorCode('W_4000',NULL,'TextDB_ChangeData');
						return false;
					}

					//获取字段个数，欲填字段
					$FieldsSize = sizeof($Fields)-1;

					//获取数据个数，欲填数据
					$ValuesSize = sizeof($Values)-1;

					//检测字段，数据是否对应
					if($FieldsSize!=$ValuesSize)
					{
						//释放文件
						TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
						TextDB_SetErrorCode('W_4000',$FieldsSize.' <> '.$ValuesSize,'TextDB_ChangeData');
						return false;
					}

					//检测字段是否对应
					if($FieldsSize<$TableFieldsSize or $Fields==0)
					{
						//获取字段ID
						for($j=0;$j<=$FieldsSize;$j++)
						{
							if($Fields[$j]!="TEXTDB_HID")
							{
								for($k=1;$k<$TableFieldsSize;$k++)
								{
									//如果字段存在，赋予ID
									if(TextDB_FilterStr($Fields[$j])==$TableFields[$k])
									{
										$Fields[$j]=$k;
									}
								}
							}
							else
							{
								$Fields[$j]=0;
							}

							//如果字段不为ID，就表明字段不存在，返回错误
							if(!is_int($Fields[$j]))
							{
								//释放文件
								TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
								TextDB_SetErrorCode('W_3003',$Fields[$j],'TextDB_ChangeData');
								return false;
							}
						}
					}
					else
					{
						//释放文件
						TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
						TextDB_SetErrorCode('W_4000',NULL,'TextDB_ChangeData');
						return false;
					}

					//分析数据
					$TempData = explode(_TextDB_EXStr_,$TableContent[$i]);

					//修改数据
					for($l=0;$l<=$FieldsSize;$l++)
					{
						$TempData[$Fields[$l]] = TextDB_FilterStr($Values[$l]);
					}
					$TableContent[$i] = implode(_TextDB_EXStr_,$TempData);

					unset($TempData);
					$IN=1;

				}
				else
				{
					//非多个字段添加
					//检测字段和数据数据类型是否对应
					if(is_array($Values))
					{
						//释放文件
						TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
						TextDB_SetErrorCode('W_4000',NULL,'TextDB_ChangeData');
						return false;
					}

					if($Fields!="TEXTDB_HID")
					{
						for($j=1;$j<$TableFieldsSize;$j++)
						{
							//如果字段存在，赋予ID
							if(TextDB_FilterStr($Fields)==$TableFields[$j])
							{
								$Fields=$j;
							}
						}
					}
					else
					{
						$Fields=0;
					}

					//如果字段不为ID，就表明字段不存在，返回错误
					if(!is_int($Fields))
					{
						//释放文件
						TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
						TextDB_SetErrorCode('W_3003',$Fields,'TextDB_ChangeData');
						return false;
					}

					//分析数据
					$TempData = explode(_TextDB_EXStr_,$TableContent[$i]);

					//修改数据
					$TempData[$Fields] = TextDB_FilterStr($Values);

					//合并数
					$TableContent[$i] = implode(_TextDB_EXStr_,$TempData);

					unset($TempData);
					$IN=1;
				}
			}
		}
	}
	//检测是否有修改数据
	if(!$IN)
	{
		//释放文件
		TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
		TextDB_SetErrorCode('N_4001',NULL,'TextDB_ChangeData');
		return false;
	}

	//分析最后的数据
	$LastData = explode(_TextDB_EXStr_,$TableContent[$TableContentSize]);

	//消除换行符号
	$LastData[sizeof($LastData)-1] = "";

	//合并最后的数据
	$TableContent[$TableContentSize] = implode(_TextDB_EXStr_,$LastData);

	$WriteContent = implode("",$TableContent);

	//写入数据
	TextDB_FileWrite($TableName,"w",$WriteContent);

	//释放文件
	TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

	return true;
}

//获取已搜索数据行数
// 创建日期 2008-8-29
// 参数说明
//   $Data 搜索返回数据
function TextDB_DataRows($Data)
{
	if(is_array($Data))
	{
		return count($Data);
	}
	else
	{
		TextDB_SetErrorCode('N_4002',NULL,'TextDB_ChangeData');
		return false;
	}
}


//获取数据
// TextDB_SearchData() 别名
function TextDB_GetData($TableName,$Fields=NULL,$Values=NULL,$Order="asc",$OrderFields=NULL,$StartNum=NULL,$Number=NULL,$TextDB_Link_DB_ID=NULL)
{
	return TextDB_SearchData($TableName,$Fields,$Values,$Order,$StartNum,$Number,$TextDB_Link_DB_ID);
}

//搜索数据
//
// 参数说明
//   $TableName   //搜索表
//   $Fields      //搜索字段
//   $Values      //搜索数值
//   $Order       //排序 顺序（ASC），倒序（DESC）,乱序（RAND）
//   $OrderFields //排序字段
//   $StartNum    //开始行
//   $Number      //获取行数
function TextDB_SearchData($TableName,$Fields=NULL,$Values=NULL,$Order="asc",$OrderFields=NULL,$StartNum=NULL,$Number=NULL,$TextDB_Link_DB_ID=NULL)
{
	$Order===NULL && $Order='asc';

	if(!TextDB_CheckInit())
	{
		TextDB_SetErrorCode('E_0001',NULL,'TextDB_SearchData');
		return false;
	}

	if(empty($TableName))
	{
		TextDB_SetErrorCode('W_2000',NULL,'TextDB_SearchData');
		return false;
	}

	$TextDB_Link_DB_ID = TextDB_GetLinkDBID($TextDB_Link_DB_ID);
	if(!$TextDB_Link_DB_ID)
	{
		/* TextDB_Link_DB_ID 不存在 */
		TextDB_SetErrorCode('E_1004',$TextDB_Link_DB_ID,'TextDB_SearchData');
		return false;
	}

	//检测数据表名称是否符合要求
	if(TextDB_CheckStr($TableName,_TextDB_SysFilter_TBN_))
	{
		TextDB_SetErrorCode('W_2002',$TableName,'TextDB_SearchData');
		return false;
	}

	$TempTableName = $TableName;
	$TableName     = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_TBL_;
	$BakTableName  = _TextDB_SQLPath_."/_".TextDB_GetLinkDBName($TextDB_Link_DB_ID)."/_".$TempTableName._TextDB_Bak_._TextDB_TBL_;

	//检测表
	if(!file_exists($TableName) && !is_file($TableName))
	{
		if(file_exists($BakTableName) && is_file($BakTableName))
		{
			TextDB_SetErrorCode('W_2007',$BakTableName,'TextDB_SearchData');
			return false;
		}
		else
		{
			TextDB_SetErrorCode('W_2003',$TempTableName,'TextDB_SearchData');
			return false;
		}
	}

	//检测文件是否被锁定
	while(TextDB_isLockTable($TempTableName,$TextDB_Link_DB_ID))
	{
		usleep(_TextDB_LLUTime_);
	}

	//锁定文件
	TextDB_LockTable($TempTableName,$TextDB_Link_DB_ID);

	//分析数据表内容
	$TableContent     = TextDB_FileToArray($TableName);
	$TableContentSize = sizeof($TableContent);

	//分析数据表结构
	$TableFields      = explode(_TextDB_EXStr_,TextDB_ReadOneLine($TableName));
	$TableFieldsSize  = sizeof($TableFields);

	//生成空数据列表
	$DataList = "";

	//获取全部数据
	if(empty($Fields) and empty($Values))  //如果搜索字段和数据为空
	{
		$m  = 0;

		for($i=1;$i<$TableContentSize;$i++)
		{
			$TempData=explode(_TextDB_EXStr_,$TableContent[$i]);

			//获取数据表表字段个数(去处开头结尾)
			$TempDataSize=sizeof($TempData)-1;
			$NewData="";

			//整理表字段
			for($j=0;$j<$TempDataSize;$j++)
			{
				$NewData[$j] = TextDB_FilterStr($TempData[$j],0);
				if($j>0)
				{
					$NewData[TextDB_FilterStr($TableFields[$j],0)] = TextDB_FilterStr($TempData[$j],0);
				}
				else
				{
					$NewData['TEXTDB_HID'] = TextDB_FilterStr($TempData[$j],0);
				}
			}
			$TempDataList[$m] = $NewData;
			unset($TempData,$NewData);
			$m++;
		}
	}
	elseif(isset($Fields) && isset($Values))  //如果搜索字段和数据不为空
	{
		//检测搜索字段是否为多个字段
		if(strstr($Fields,","))
		{
			$Fields = explode(",",$Fields);
		}

		//模拟字段数组
		if(is_array($Fields))
		{
			//多个字段添加
			//检测字段和数据数据类型是否对应
			if(!is_array($Values))
			{
				//释放文件
				TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
				TextDB_SetErrorCode('W_4000',NULL,'TextDB_SearchData');
				return false;
			}

			//获取字段个数，欲填字段
			$FieldsSize = sizeof($Fields)-1;

			//获取数据个数，欲填数据
			$ValuesSize = sizeof($Values)-1;

			//检测字段，数据是否对应
			if($FieldsSize!=$ValuesSize)
			{
				//释放文件
				TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
				TextDB_SetErrorCode('W_4000',$FieldsSize.' <> '.$ValuesSize,'TextDB_SearchData');
				return false;
			}

			//检测字段是否对应
			if($FieldsSize<$TableFieldsSize or $Fields==0)
			{
				//获取字段ID
				for($i=0;$i<=$FieldsSize;$i++)
				{
					if($Fields[$i]!="TEXTDB_HID")
					{
						for($j=1;$j<$TableFieldsSize;$j++)
						{
							//如果字段存在，赋予ID
							if(TextDB_FilterStr($Fields[$i])==$TableFields[$j])
							{
								$Fields[$i] = $j;
							}
						}
					}
					else
					{
						$Fields[$i] = 0;
					}

					//如果字段不为ID，就表明字段不存在，返回错误
					if(!is_int($Fields[$i]))
					{
						//释放文件
						TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
						TextDB_SetErrorCode('W_3003',$Fields[$i],'TextDB_SearchData');
						return false;
					}
				}
			}
			else
			{
				//释放文件
				TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
				TextDB_SetErrorCode('W_4000',NULL,'TextDB_SearchData');
				return false;
			}

			//查找数据并返回
			$m  = 0;

			for($i=1;$i<$TableContentSize;$i++)
			{
				//设置字段检测变量
				$k=0;
				$TableData=explode(_TextDB_EXStr_,$TableContent[$i]);
				//检测是否有符合多条件数据
				for($j=0;$j<=$FieldsSize;$j++)
				{
					if(TextDB_CheckWhere(TextDB_FilterStr($Values[$j]),$TableData[$Fields[$j]]))
					{
						$k+=1;
					}
				}

				//测试数据-1，同等sizeof($Fields)-1
				if($k-1==$FieldsSize)
				{
					//分析数据表结构
					$TempData = explode(_TextDB_EXStr_,$TableContent[$i]);

					//获取表字段个数
					$TempDataSize = sizeof($TempData)-1;
					$NewData = "";

					//整理表字段
					for($l=0;$l<$TempDataSize;$l++)
					{
						$NewData[$l]=TextDB_FilterStr($TempData[$l],0);
						if($l>0)
						{
							$NewData[TextDB_FilterStr($TableFields[$l],0)] = TextDB_FilterStr($TempData[$l],0);
						}
						else
						{
							$NewData['TEXTDB_HID'] = TextDB_FilterStr($TempData[$l],0);
						}
					}
					$TempDataList[$m] = $NewData;
					unset($TempData,$NewData);
					$m+=1;
				}
			}
		}
		else
		{
			//非多个字段添加
			//检测字段和数据数据类型是否对应
			if(is_array($Values))
			{
				//释放文件
				TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
				TextDB_SetErrorCode('W_4000',NULL,'TextDB_SearchData');
				return false;
			}

			if($Fields!="TEXTDB_HID")
			{
				for($i=1;$i<$TableFieldsSize;$i++)
				{
					//如果字段存在，赋予ID
					if(TextDB_FilterStr($Fields)==$TableFields[$i])
					{
						$Fields = $i;
					}
				}
			}
			else
			{
				$Fields = 0;
			}

			//如果字段不为ID，就表明字段不存在，返回错误
			if(!is_int($Fields))
			{
				//释放文件
				TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
				TextDB_SetErrorCode('W_3003',$Fields,'TextDB_SearchData');
				return false;
			}

			//查找数据并返回
			$k=0;
			for($i=1;$i<$TableContentSize;$i++)
			{
				$TableData=explode(_TextDB_EXStr_,$TableContent[$i]);
				if(isset($TableData[$Fields]) && TextDB_CheckWhere($Values,$TableData[$Fields]))
				{
					//分析数据表结构
					$TempData = explode(_TextDB_EXStr_,$TableContent[$i]);

					//获取表字段个数
					$TempDataSize = sizeof($TempData)-1;
					$NewData      = "";

					//整理表字段
					for($j=0;$j<$TempDataSize;$j++)
					{
						$NewData[$j]=TextDB_FilterStr($TempData[$j],0);
						if($j>0)
						{
							$NewData[TextDB_FilterStr($TableFields[$j],0)] = TextDB_FilterStr($TempData[$j],0);
						}
						else
						{
							$NewData['TEXTDB_HID'] = TextDB_FilterStr($TempData[$j],0);
						}
					}
					$TempDataList[$k] = $NewData;
					unset($TempData,$NewData);
					$k+=1;
				}
			}
		}
	}

	//是否按照字段排序
	if(!empty($OrderFields) && isset($TempDataList))
	{
		$TempDataList = TextDB_OrderData($TempDataList,$OrderFields);
	}

	//检测是否返回数据，返回找到数据
	if(isset($TempDataList) && (isset($TempDataList[0]['TEXTDB_HID']) || TextDB_CheckStr($TempTableName,_TextDB_SysTBSN_,1)))
	{
		//判断数据数据取出顺序 并计算位置
		$Order = strtolower($Order);  //容错处理

		//数据大小
		$TempDataSize = sizeof($TempDataList);
		$n  = 0;

		//依照顺序，和数量调取
		if($Order=='asc')
		{
			//获取开始位置
			$StartNum = empty($StartNum) ? 0 : $StartNum;

			//获取结束位置
			$Number = empty($Number) ? $TempDataSize : $StartNum+$Number;
			$Number = $Number>$TempDataSize ? $TempDataSize : $Number;

			//结束位置必须大于等于开始位置
			if($Number<$StartNum)
			{
				//释放文件
				TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
				TextDB_SetErrorCode('W_4001',NULL,'TextDB_SearchData');
				return false;
			}

			//列出数据
			for($i=$StartNum;$i<$Number;$i++)
			{
				$DataList[$n] = $TempDataList[$i];
				$IN = 1;
				$n++;
			}
		}
		elseif($Order=='desc')
		{
			//结束位置必须小于等于开始位置
			if($StartNum>$TempDataSize-1)
			{
				//释放文件
				TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
				TextDB_SetErrorCode('W_4002',NULL,'TextDB_SearchData');
				return false;
			}

			//获取开始位置
			$StartNum = empty($StartNum) ? ($TempDataSize-1) : ($TempDataSize-$StartNum-1);
			$StartNum = $StartNum>0 ? $StartNum : 0 ;

			//获取结束位置
			$Number = empty($Number) ? 0 : $StartNum-$Number+1;
			$Number = $Number<=0 ? 0 : $Number;

			//结束位置必须大于开始位置
			if($Number>$StartNum)
			{
				//释放文件
				TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
				TextDB_SetErrorCode('W_4003',NULL,'TextDB_SearchData');
				return false;
			}

			//列出数据
			for($i=$StartNum;$i>=$Number;$i--)
			{
				$DataList[$n] = $TempDataList[$i];
				$IN = 1;
				$n++;
			}
		}
		elseif($Order=='rand')
		{
			//打乱数据内容
			for($i=0;$i<$TempDataSize;$i++)
			{
				$randID = rand(0,$TempDataSize-1);
				if(empty($_TempList[$randID]['TEXTDB_HID']))
				{
					$_TempList[$randID] = $TempDataList[$i];
				}
				else
				{
					$i-=1;
				}
			}
			$TempDataList = $_TempList;
			unset($_TempList);


			//获取开始位置
			$StartNum = empty($StartNum) ? 0 : $StartNum;

			//获取结束位置
			$Number = empty($Number) ? $TempDataSize : $StartNum+$Number;
			$Number = $Number>$TempDataSize ? $TempDataSize : $Number;

			//结束位置必须大于等于开始位置
			if($Number<$StartNum)
			{
				//释放文件
				TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);
				TextDB_SetErrorCode('W_4001',NULL,'TextDB_SearchData');
				return false;
			}

			//列出数据
			for($i=$StartNum;$i<$Number;$i++)
			{
				$DataList[$n] = $TempDataList[$i];
				$IN = 1;
				$n++;
			}
		}
		else
		{
			//释放文件
			TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

			TextDB_SetErrorCode('W_4004',$TempTableName,'TextDB_SearchData');

			//未知排序
			return false;
		}

		//释放文件
		TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

		//返回数据
		unset($TempDataList);
		return $DataList;
	}
	else
	{
		//释放文件
		TextDB_UnlockTable($TempTableName,$TextDB_Link_DB_ID);

		//无数据
		unset($TempDataList);

		TextDB_SetErrorCode('N_4003',$TempTableName,'TextDB_SearchData');

		return false;
	}
}
?>